Print specific range of numbers in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Print specific range of numbers in a List in Python #

To print a specific range of numbers in a list:

  1. Use the range() class to get a range of numbers.
  2. Use the list() class to convert the range to a list.
  3. Use the print() function to print the list.
main.py
list_of_numbers = list(range(1, 11)) print(list_of_numbers) # 👉️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(*list_of_numbers) # 👉️ 1 2 3 4 5 6 7 8 9 10 print(*list_of_numbers, sep=',') # 👉️ 1,2,3,4,5,6,7,8,9,10

We used the range() class to get a range of numbers.

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
for n in range(5): print(n) result = list(range(5)) # 👇️ [0, 1, 2, 3, 4] print(result)
The example shows that if the start argument is omitted, it defaults to 0 and if the step argument is omitted, it defaults to 1.

If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive.

main.py
result = list(range(1, 5)) # 👇️ [1, 2, 3, 4] print(result)

The last step is to use the list() class to convert the range object to a list.

If you need to print the list's elements with a specific separator, use the sep argument in the call to print().

main.py
list_of_numbers = list(range(1, 11)) print(list_of_numbers) # 👉️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(*list_of_numbers) # 👉️ 1 2 3 4 5 6 7 8 9 10 print(*list_of_numbers, sep=',') # 👉️ 1,2,3,4,5,6,7,8,9,10 print(*list_of_numbers, sep='-') # 👉️ 1-2-3-4-5-6-7-8-9-10
Notice that we used the iterable unpacking * operator to unpack the items of the list in the call to print().

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

The sep argument is the separator between the arguments we pass to print().

main.py
print('bobby', 'hadz', sep='') # 👉️ bobbyhadz print('bobby', 'hadz') # 👉️ bobby hadz

By default, the sep argument is set to a space.

You can use the str.join() method if you need to join the list of numbers into a string with a specific separator.

main.py
list_of_numbers = list(range(1, 11)) result = ', '.join(str(item) for item in list_of_numbers) print(result) # 👉️ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

This is why we had to convert each number to a string in the call to join().

The string the method is called on is used as the separator between the elements.

main.py
list_of_numbers = list(range(1, 11)) result = ' '.join(str(item) for item in list_of_numbers) print(result) # 👉️ 1 2 3 4 5 6 7 8 9 10
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.