In Python, a list is a collection of objects. List is a data structure used to store the data. Some other data structures that are used to store the data are Tuple, Set, and Dictionary. The simplest type is a singleton list, which contains only one element. In this article, we will learn how to print the elements of a list in python.
Methods to Print the elements of a list in python
You can print the elements of the list using different functions such as the join(), Unpacked function, for loop method, and even map().
Method 01: Using For Loop to print list elements
The most simplest method is to use for loop to print the elements of a list in a separate line. Traverse from 0th index i.e., the starting point of the list to the last index and print them one by one on every iteration.
#create an empty list
list1 =[]
#Enter the number of elements in list
x = int(input("Enter number of elements : "))
#Take element as an input from the user and append it in list1
print("Enter elements: ")
for i in range(x):
item = int(input())
# Add the item in the list
list1.append(item)
#print the whole list in a single line
print("Input List: ",list1)
#print the list elements in a separate line
print("Displaying list elements in a separate line")
for j in range(len(list1)):
print("Item ",j+1, ": ", list1[j])
Enter number of elements : 5
Enter elements:
12
42
27
56
34
Input List: [12, 42, 27, 56, 34]
Displaying list elements in a separate line
Item 1 : 12
Item 2 : 42
Item 3 : 27
Item 4 : 56
Item 5 : 34
Method 02: Join method to print element in the list
In some applications, you want to display list elements to form a single sentence. Python provides a built-in join function which takes all the elements in an iterable and join them together in the form of a string by inserting a separator between them. This separator can be empty space or any special character.
# Create a list consisting of strings as an element
list1 = []
#Enter the number of elements in list
x = int(input("Enter number of elements : "))
#Take element as an input from the user and append it in list1
print("Enter elements: ")
for i in range(x):
item = str(input())
# Add the item in the list
list1.append(item)
#print the whole list in a single line
print("Input List: ",list1)
# Using empty space as a separator in join() function.
separator=" "
print(separator.join(list1))
Enter number of elements : 2
Enter elements:
Hello
World
Input List: ['Hello', 'World']
Hello World
Lets see another example in which we have a list of numbers and we want to join them with sum ‘+’ operator.
list1=['1', '2', '3', '4', '5', '7', '9']
#Using sum(+) operator as a separator in join() function.
separator=" + "
print(separator.join(list1))
1 + 2 + 3 + 4 + 5 + 7 + 9
Keep in mind one thing, the join function only join the list of string as an element.
Method 03: USing Map method to print element in the list
The map method is used to map the list elements in the string format for readability. The map() function requires two arguments: the function which it applies on the element, and the input list on which the function is applied. When function is applied to each item in an iterable, it returns a map object (which is an iterator). Consider an example in which we have used lambda and str.upper() function which capitalizes all list elements and then map function map the list elements into string. Then, we have joined all the elements using join() method.
#initialize a list
input_list = ['welcome','to','entechin']
print(' '.join(map(lambda x:str.upper(x), input_list)))
WELCOME TO ENTECHIN
We have discussed different methods to print list elements depending upon the application requirements. We have used join(), map() and for loop along with examples. If you have any query, please let us know in the comments. Your feedback matters a lot for us. Contact us.