Python

Python Print List

As we all know, the list data structure is well-known within the Python language. The list data structure can be filled with many sequences of elements. As a plus point, the list data structure maintains the proper order of the values. The most known print statement can be used to display the list items on the console. Within this guide, we will be discussing a few different methods in our examples to print out the list. Let’s have a look at each one of them. Make sure to have Spyder 3 already configured on your system.

Method 01: Print Statement

The first and easy method to print out the list on the console is the “print” statement. So, open the Spyder 3 tool and create a new Python project, i.e., temp.py. Once the project has been opened, initialize a mixed list having some float and string values in it. The print statement is initialized, which takes the list in its argument to print it out. You can copy the code that is written below:

list = [ 0.1, 0.2 ,0.3 ,'kalsoom' ,'akhtar' ]

print( "List: “, List )

Let’s run the Python code by tapping on the “Run” button from the top taskbar of the tool. You can see, the whole list has been printed out:

Method 2: Map() Function

One of the many methods to print out the list data structure in the Python console is using the “Map()” function. The map function can be utilized in many ways, such as to convert the uppercase elements of a list to a lowercase. It can also be utilized to convert the list to a string and display it. The print statement has been used to tell that the list elements are going to be displayed. On the 3rd line, we have converted the whole list to a string using the map() function.

The join() method is used in combination with map functions to again assemble the values of a list. The print statement will print out each value separately at the next line by the use of “\n”. You can copy the following code provided:

list = [ 0.1 ,0.2 ,0.3 ,'kalsoom' ,'akhtar' ]

print("Check out the list elements:\n" )

print( '\n'.join(map(str, list)))

When we have executed the list via the “Run” sign on the taskbar, it shows each element as a string on each separate line. This is due to the usage of the join() and map() function together.

Method 3: Use Of “*” Character

Let’s look at the other “*” method to print out the list elements on the console separately. Open the same project and make changes at the last print line. Don’t make changes to any other line in the code. The last print statement has been taking two arguments in it, i.e., the list and the separator. The list has been defined with a “*” character. The separator has been assigned with the “\n” to give a new line. You can copy the following code:

list = [ 0.1 ,0.2 ,0.3 ,'kalsoom' ,'akhtar' ]

print( "Check out the list elements:\n" )

print(*list, sep = "\n")

After running and debugging this updated code, we have got the list elements displayed on the Spyder 3 console, each one at a separate line:

Method 04: Naïve Way

The Naïve way is quite popular among Python users as it utilizes the simple and handy “for” loop so far. Thus, we have been utilizing it as well. Use the same code project and update the last print line. Before the print line, we have added the “for” loop to iterate each element of a list. Then display it on the shell with the help of a “print” statement. You can copy the code that is written below:

list = [ 0.1 ,0.2 ,0.3 ,'kalsoom' ,'akhtar' ]

print("Check out the list elements:\n")

for x in list:

print(x)

After running this updated code, we have the same result as we derived in the previous examples.

Conclusion:

At last, we have completed the four short, easy, and simple methods to get the list printed out at the console of the Python tool, i.e., Spyder 3. The advice relevant to this article is to implement all these approaches on your systems. That’s all for the print list in Python, and please check out the other helpful articles in Linux Hint.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content