Loop through a list Python

for loop in Python

List is a collection of objects, so we can loop through the list and extract the objects each at a time. consider the following to know more.


Iterate over a list using 'for' loop in Python

The 'for' loop in Python is different form the 'for' loop in the other programming languages.


In Python, the 'for' loop does not require any update to be done. It automatically update after each iteration.


Python 'for' loop syntax

for variable in list_name:
    statements

Example

Input

a = [1, 2, 3, 4, 5]
for i in a:
    print(i)

Output

1
2
3
4
5



'for' loop over list with range() method

Loop over a list, like above can be done with the range() method.


Program

Input

a = [1, 2, 3, 4, 5]
length = len(a)
for i in range(0, length):
    print(a[i])

Output

1
2
3
4
5


In looping over a list, range() method gives us the flexibility to slice the list.

Program

Input


b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for j in range(2, 5):
    print(b[j])


Output


3
4
5



Interval can be introduced while looping over a  list with the range() method.

Program

Input


b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for step in range(0, length, 2):
    print(a[step])


Output

1
3
5


'for' loop over list with enumerate() method

The enumerate() method enumerates the list, with numbers.


Following is an example to enumerate the list.

Program


>>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(enumerate(a))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]



Here is an example, how you can fetch the content, got after enumerating the list. Both, list value and the enumerated value.

Program

Input


b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i, j in list(enumerate(b)):
    print("Index: ", i, " Value: ", j
)


Output

Index:  0  Value:  1
Index:  1  Value:  2
Index:  2  Value:  3
Index:  3  Value:  4
Index:  4  Value:  5



List comprehension

List comprehension with 'for' loop gives a lot of information with 1 line of sentence and is more easy and readable.



Creating a list using list comprehension with 'for' loop

Following is the program which will create a list without using list comprehension mechanism.

Program


>>> a = []
>>> for value in range(0, 10):
a.append(value)

>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Now this is how you can create a list using list comprehension with just one line of code. Which is so easy.

Program


>>> compre = [value for value in range(0, 10)]
>>> print(compre)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


List comprehension with an expression

Let's put an expression at the beginning, for some computation for the list comprehension.


Here is an example, which will raise each value with the power of 2.

Program


>>> a = [1, 2, 3]
>>> [x**2 for x in a]
[1, 4, 9]



List / tuple collection with list comprehension

Creation of list which will be the collection of list / tuple, using list comprehension.


The following code will have the collection of tuples, in which the two elements of each tuple will not be equal to each other.

Program


>>> col = [(x, y) for x in range(0, 5) for y in range(0, 5) if x!=y]
>>> print(col)
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3)]


List comprehension with a function

Introducing a function at the beginning of the for loop in list comprehension, is also possible.


List comprehension with built-in function

Here is an example, Which will exponentiate each value with 2.

Program


>>> a = [1, 2, 3]
>>> pow2 =[pow(value, 2) for value in a]
>>> print(pow2)
[1, 4, 9]


List comprehension using the user defined function

First of all we define a function, and then introduce it into the list comprehension.

Program

Input


def add2(x):
    return x + 2

a = [1, 2, 3, 4, 5]

add_with_2 = [add2(value) for value in a]

print(add_with_2)


Output


[3, 4, 5, 6, 7]




Conclusion

Here we have learned how we can loop through a list, enumerated list and list comprehension.





If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)