Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they’ve been created.
We’ll look at a few different ways to iterate through a list in this article.
Examples:
Input:
givenlist=['hello' , 'world' , 'this', 'is' ,'python']
Output:
hello world this is python
Print the list by iterating through it
There are several ways to iterate through a list some of them are:
- Using For loop
- Using while loop
- Using For loop and range() function
- Using list comprehension
- Using enumerate()
Method #1 : Using For loop
We can use for loop to iterate over the list.
Below is the implementation:
# Given list givenlist=['hello' , 'world' , 'this', 'is' ,'python'] #Using for loop to iterate over the list for element in givenlist: print(element)
Output:
hello world this is python
Method #2 :Using while loop
In Python, the while loop is used to iterate through a block of code as long as the test expression (condition) is true. This loop is typically used when we don’t know how many times to iterate ahead of time.
Here the condition is loop till length of list
Below is the implementation:
# Given list givenlist = ['hello', 'world', 'this', 'is', 'python'] # Getting length of list length = len(givenlist) # Initializing position to index 0 position = 0 # Iterating using while loop while position < length: print(givenlist[position]) position = position + 1
Output:
hello world this is python
Method #3: Using For loop and range() function
If we want to use the standard for loop, which iterates from x to y, we can do so.
Below is the implementation:
# Given list givenlist = ['hello', 'world', 'this', 'is', 'python'] # Getting length of list length = len(givenlist) # Iterating till length of list for index in range(length): print(givenlist[index])
Output:
hello world this is python
Method #4 : Using list comprehension
We can iterate over the list in one line using list comprehension
Below is the implementation:
# Given list givenlist = ['hello', 'world', 'this', 'is', 'python'] # Using list comprehension [print(i) for i in givenlist]
Output:
hello world this is python
Method #5 :Using enumerate()
The enumerate() function can be used to transform a list into an iterable list of tuples or to get the index based on a condition check.
Below is the implementation:
# Given list givenlist = ['hello', 'world', 'this', 'is', 'python'] # Using enumerate() for index, value in enumerate(givenlist): print(value)
Output:
hello world this is python
Related Programs:
- python different ways to iterate over a list in reverse order
- how to create and initialize a list of lists in python
- python how to find all indexes of an item in a list
- how to convert a list to dictionary in python
- python how to create a list and initialize with same values
- python how to sort a list of strings list sort
- python tuple different ways to create a tuple and iterate over it