Learn to print a List in Python using different ways such as with/without square brackets, with/without the separator, curly braces, and custom formatting with examples.
Method Name | Syntax | Example Output |
---|---|---|
Print List without Square Brackets and Separator | print(*my_list) | 1 2 3 4 5 |
Print List without Square Brackets but with Separator | print(‘, ‘.join(map(str, my_list))) | 1, 2, 3, 4, 5 |
Print List with Square Brackets | print(num_list) print(num_list, sep=”, “, end=”\n”) print(‘[{}]’.format(‘, ‘.join(map(str, num_list)))) | [1, 2, 3, 4, 5] |
Print List with Curly Braces separated by Commas | print(‘{{{}}}’.format(‘, ‘.join(map(str, my_list)))) | {1, 2, 3, 4, 5} |
Print List with a Specified Separator | print(‘ | ‘.join(map(str, my_list))) | 1 | 2 | 3 | 4 | 5 |
Print List with Custom Formatting | print(‘Numbers: {}’.format(‘, ‘.join(map(str, my_list)))) | Numbers: 1, 2, 3, 4, 5 |
Print List Item Index and Value | for i in range(len(my_list)): print(“Item index is”, i, “and Item is”, my_list[i]) | Item index is 0 and Item is 1 … so on |
1. Print List without Square Brackets and Separator
To remove the square brackets from the print output, we utilize the unpacking operator (*) which prints each element of the list separated by a space, without enclosing brackets.
print(*my_list)
# Output: 1 2 3 4 5
2. Print List without Square Brackets but with Separator
In this example, we join the elements of the list as strings separated by commas using the join() method.
print(', '.join(map(str, my_list)))
# Output: 1, 2, 3, 4, 5
3. Print List with Square Brackets
We can print a Python list with square brackets in several ways. The following example demonstrates how to do this in 3 ways. Each method produces the same output.
The 3rd method uses the join() which concatenates the elements of the list as strings separated by commas. We are using the map() method to convert to int value to strings.
print(num_list)
print(num_list, sep=", ", end="\n")
print('[{}]'.format(', '.join(map(str, num_list))))
# Output: [1, 2, 3, 4, 5]
4. Print List with Curly Braces separated by Commas
To print a list formatted with curly braces, we use the string formatting and the join() method to concatenate the elements as strings separated by commas.
Note that format() method is used to insert the joined string into the placeholder {}
. However, to print the curly braces themselves, we need to use double curly braces {{
and }}
within the format string to escape them.
print('{{{}}}'.format(', '.join(map(str, my_list))))
# Output: {1, 2, 3, 4, 5}
5. Print List with a Specified Separator
In this example, the list elements are joined as strings separated by a specified separator (‘ | ‘) using the join() method.
print(' | '.join(map(str, my_list)))
# Output: 1 | 2 | 3 | 4 | 5
6. Print List with Custom Formatting
We can format the list with a custom message (‘Numbers:’) and join the elements of the list as strings separated by commas using string formatting.
print('Numbers: {}'.format(', '.join(map(str, my_list))))
# Output: Numbers: 1, 2, 3, 4, 5
7. Print List Item Index and Value
Python program to print the index and the items of a List using a for loop using the range() method.
for i in range(len(my_list)):
print("Item index is", i, "and Item is", my_list[i])
Program output.
Item index is 0 and Item is 1
Item index is 1 and Item is 2
Item index is 2 and Item is 3
Item index is 3 and Item is 4
Item index is 4 and Item is 5
8. Print a Sublist
Python example which uses the array slicing method to print a subset of items from a List. Note that indices start from 0
.
The from
index is inclusive and to
index is exclusive.
print(my_list[1:3])
Program output.
[1, 2]
Happy Learning !!