Last Updated : 07 Jul, 2022
Output:
1 3 5 7 9
In this syntax, the for loop statement assigns an individual element of the list to the item variable in each iteration.,In this example, the for loop assigns an individual element of the cities list to the city variable and prints out the city in each iteration.,Inside the body of the loop, you can manipulate each list element individually.,To access the index, you can unpack the tuple within the for loop statement like this:
To iterate over a list, you use the for
loop statement as follows:
.wp - block - code {
border: 0;
padding: 0;
}
.wp - block - code > div {
overflow: auto;
}
.shcb - language {
border: 0;
clip: rect(1 px, 1 px, 1 px, 1 px); -
webkit - clip - path: inset(50 % );
clip - path: inset(50 % );
height: 1 px;
margin: -1 px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1 px;
word - wrap: normal;
word - break: normal;
}
.hljs {
box - sizing: border - box;
}
.hljs.shcb - code - table {
display: table;
width: 100 % ;
}
.hljs.shcb - code - table > .shcb - loc {
color: inherit;
display: table - row;
width: 100 % ;
}
.hljs.shcb - code - table.shcb - loc > span {
display: table - cell;
}
.wp - block - code code.hljs: not(.shcb - wrap - lines) {
white - space: pre;
}
.wp - block - code code.hljs.shcb - wrap - lines {
white - space: pre - wrap;
}
.hljs.shcb - line - numbers {
border - spacing: 0;
counter - reset: line;
}
.hljs.shcb - line - numbers > .shcb - loc {
counter - increment: line;
}
.hljs.shcb - line - numbers.shcb - loc > span {
padding - left: 0.75 em;
}
.hljs.shcb - line - numbers.shcb - loc::before {
border - right: 1 px solid #ddd;
content: counter(line);
display: table - cell;
padding: 0 0.75 em;
text - align: right; -
webkit - user - select: none; -
moz - user - select: none; -
ms - user - select: none;
user - select: none;
white - space: nowrap;
width: 1 % ;
}
for item in list:
# process the itemCode language: Python(python)
For example, the following defines a list of cities and uses a for
loop to iterate over the list:
cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico']
for city in cities:
print(city) Code language: Python(python)
Output:
New York
Beijing
Cairo
Mumbai
MexicoCode language: Shell Session(shell)
To access the index, you can unpack the tuple within the for
loop statement like this:
cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico']
for index, city in enumerate(cities):
print(f "{index}: {city}") Code language: Python(python)
The following example uses the enumerate()
function with the index that starts from one:
cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico']
for index, city in enumerate(cities, 1):
print(f "{index}: {city}") Code language: Python(python)
list = [1, 2, 3, 4, 5] # Iterating list using for loop for i in list: print(i)
list = [1, 2, 3, 4, 5] # getting length of list using len() function length = len(list) # using for loop along with range() function for i in range(length): print(list[i])
list = [1, 2, 3, 4, 5] # Getting length of list using len() function length = len(list) i = 0 while i < length: print(list[i]) i += 1
list = [1, 2, 3, 4, 5] # Iterating list using list comprehension [print(i) for i in list]
list = [1, 3, 5, 7, 9] # Using enumerate() function to iterate the list for i, val in enumerate(list): print(i, ",", val)
# Importing external library import numpy as np a = np.arange(5) for x in np.nditer(a): print(x)
Using Python range() method,Using Python enumerate() method,Using Python NumPy module, Understanding Tracebacks in Python
range(start, stop[, step])
lst = [10, 50, 75, 83, 98, 84, 32] for x in range(len(lst)): print(lst[x])
10 50 75 83 98 84 32
for var_name in input_list_name:
lst = [10, 50, 75, 83, 98, 84, 32] for x in lst: print(x)
[expression / statement for item in input_list ]
Using the built-in function zip also lets you iterate in parallel: ,The loop runs three times. On the last iteration, y will be None. ,Using the built-in function map, with a first argument of None, you can iterate on both lists in parallel: ,zip lets you iterate over the lists in a similar way, but only up to the number of elements of the smallest list. Therefore, the output of the second technique is:
last modified July 29, 2022
#!/usr/bin/python words = ["cup", "star", "falcon", "cloud", "wood", "door"] for word in words: print(word)
The example goes over the elements of a list of words with the for
statement.
$. / list_loop_for.py
cup
star
falcon
cloud
wood
door
#!/usr/bin/python words = ["cup", "star", "falcon", "cloud", "wood", "door"] for word in words: print(word) else: print("Finished looping")
#!/usr/bin/python words = ["cup", "star", "falcon", "cloud", "wood", "door"] for idx, word in enumerate(words): print(f "{idx}: {word}")
With the help of the enumerate
function, we print
the element of the list with its index.
$. / list_loop_enumerate.py
0: cup
1: star
2: falcon
3: cloud
4: wood
5: door
Pass two loop variables index and val in the for loop. You can give any name to these variables.,Print the required variables inside the for loop block.,Using a for loop, iterate through the length of my_list. Loop variable index starts from 0 in this case.,In this example, you will learn to access the index of a list using a for loop.
Example 1: Using enumerate
my_list = [21, 44, 35, 11] for index, val in enumerate(my_list): print(index, val)
Output
0 21 1 44 2 35 3 11
Example 2: Start the indexing with non zero value
my_list = [21, 44, 35, 11] for index, val in enumerate(my_list, start = 1): print(index, val)