Python print list
In this post, you will learn different ways to print the list using Python.
A list is a sequence of indexed and ordered values, like a dynamic size array. So that we can store multiple items in a single variable. It is mutable, which means we can change the order of elements in a list. It contains a list of any type of data objects with a comma separated and enclosed within a square bracket. The elements in a list are indexed, the first item has index[0], the second item has index[1] etc. In the development process, we may come to the situation where we need to print list items. The criteria of printing the list can be different, like – to print a specific element, to print a range of elements, to print all elements and so on. There are various ways to print a list. Here, we have mentioned most of the ways with simple examples.
Print complete list
Here, we are printing the complete list using the print() statement of Python.
# Printing elements in a list
x = [65, 34, 54, 64, 23, 46]
print("Elements in the list are : ")
print(x)
Output of the above code -
Elements in this List are :
[65, 34, 54, 64, 23, 46]
Printing list elements separated by space
Here, we are using (*) symbol to print a list of items separated by space.
# Printing elements in a list
x = ['car', 'bus', 'cycle', 'truck', 'bike']
print("Elements in the list are : ")
print(*x)
Output of the above code -
Elements in the list are :
car bus cycle truck bike
Printing list elements separated by comma
Here, we are printing a list of items separated by a comma.
# Printing elements in a list
x = ['table', 'laptop', 'mouse', 'keyboard', 'cable']
print("Elements in the list are : ")
print(*x, sep=", ")
Output of the above code -
Elements in the list are :
table, laptop, mouse, keyboard, cable
Printing lists in new line
We can use the sep attribute to print lists in a new line -
# Printing elements in a list
x = ['table', 'laptop', 'mouse', 'keyboard', 'cable']
print("Elements in the list are : ")
print(*x, sep="\n")
Output of the above code -
Elements in the list are :
table
laptop
mouse
keyboard
cable
Printing list elements using for loop
Here, we are printing all the list elements one by one using a for loop.
# Printing elements in a list
vehicles = ['car', 'bus', 'cycle', 'truck', 'bike']
print("Elements in the list are : ")
for vehicle in vehicles:
print(vehicle)
Output of the above code -
Elements in the list are :
car
bus
cycle
truck
bike
Printing ith element of the list
We can print the ith element of the list using their index value -
# Printing elements in a list
x = ['table', 'laptop', 'mouse', 'keyboard', 'cable']
print("2nd Element of the list : ", x[1])
print("5th Element of the list : ", x[4])
Output of the above code -
2nd Element of the list : laptop
5th Element of the list : cable
Printing list two times
# Printing elements in a list
x = ['table', 'laptop', 'mouse', 'keyboard', 'cable']
print(x * 2)
Output of the above code -
['table', 'laptop', 'mouse', 'keyboard', 'cable', 'table', 'laptop', 'mouse', 'keyboard', 'cable']
Printing list using join() and map() methods
The join() method returns a string in which the elements of the list have been joined by a string separator, e.g., space, comma etc.
# list
x = ['table', 'laptop', 'mouse', 'keyboard', 'cable']
# Join list items
print(' '.join(x))
Output of the above code -
table laptop mouse keyboard cable
Related Articles
Convert string to list Python
Convert array to list Python
Multiply all elements in list Python
Python multiline string
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python compare two lists
Insert data in MySQL database from an HTML form using Django
Alphabet pattern programs in Python