Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

3 Easy Methods to Print a Python List

Different Ways To Print A Python List

Hello, readers! In this article, we will be focusing on Different Ways to Print a Python list. So, let us get started!


First, what is a Python List?

Python offers us with various data structures to store and process the data. List is one of them.

Python List is data structure that stores mutable sequence of data values into it. Moreover, lists can be considered as an ordered collection of elements i.e. they follow the order of the elements.

Let us now focus on some of the Techniques to Print the elements of a List.


1. Using map() function to print a Python List

Python map() function can be clubbed with join() function to print a Python list easily.

Syntax:

''.join(map(str,list))

Example:

lst = [10,20,30,'John',50,'Joe']
print("Elements of the List:\n")
print('\n'.join(map(str, lst))) 

Explanation:

  • At first, we apply map method and convert the values present in the list to string that is we map them to string format.
  • Then, we apply join method to assemble the elements and include a newline to separate the elements.

Output:

Elements of the List:

10
20
30
John
50
Joe

2. Using ‘*’ symbol to print a Python list

Next, we will be using Python ‘*’ symbol to print a list.

Syntax:

*list

We can customize the output by including sep value. Below, we have set the separation value to a newline.

Example:

lst = [10,20,30,'John',50,'Joe'] 
print("Elements of the List:\n")
print(*lst, sep = "\n") 

Output:

Elements of the List:

10
20
30
John
50
Joe

3. Naïve Method- Using for loop

As a beginner, the Naïve method is always the best to get started!

In this method, we iterate over every element of the list using a for loop and then print a Python list here.

Syntax:

for element in list:
    print(element)

Example:

lst = [10,20,30,'John',50,'Joe'] 
print("Elements of the List:\n")
for x in lst:
    print(x)

Output:

Elements of the List:

10
20
30
John
50
Joe


Conclusion

So, as witnessed above, these were the different ways of Printing a Python list. Of course, we can use many more methods for the same. But I believe, method 1 and 2(discussed above) still stands out to be exceptional.

By this we have come to the end of this topic. For more such topics related to Python, Stay tuned and till then, Keep Learning and Growing with us!! 🙂