How to print each item from a Python list?
A list in Python may look similar like an array. A list is a collection that is ordered and also changeable. In Python programming language, lists are written with square brackets.
Below is an example of a list in Pythion:
list = ['red', 'blue', 'yellow', 'pink', 'green']
A single list can contain various data types like Integers, Strings, as well as Objects. Lists are very useful in Python and used highly in projects.
You also may need to separate each list element. So, in this post, I am going to tell you how to print each item from a Python list.
- Duplicate Elements Removal of an Array or List using Python 3.6
- How to get first N items from a list in Python
Print each list element in Python
Before we go forward, at first, let’s first create our list and take it in our variable:
list = ['Geeks', 'Red', 26, 'Phone', 76, 'CodeSpeedy']
As you can see that the above list contains both string and integer. This is because lists in Python can hold various types of data.
Now, printing each list item from our list is an easy task. Here in this example we just need to write two lines of code to print each element from our Python list. Below you can see the code:
for item in list: print(item)
These two lines of code you can see above are enough to print each element of our Python list. Now below is the complete code:
list = ['Geeks', 'Red', 26, 'Phone', 76, 'CodeSpeedy'] for item in list: print(item)
You can copy the above code and run it on your system to see the result.
As you can see, we have used for loop to print each item from a Python list. Each of the item from our list comes inside our for loop variable “item”. Then we just have to print this variable to print each element from our Python list.