10 ways to iterate through a list in python
The list is similar to array in other languages except for python, which provides the extra benefit of being dynamic in size nature. In Python, the list is a type of Data Structures, which is used to store different and multiple kinds of data at the same time.![]() |
| 10 ways to iterate through a list in python |
There are many ways to iterate over a list in Python language. Let’s see all different ways in python:
- Using For loop in python
- For loop and range() in python
- Using while loop in python
- Using list comprehension ways in python
- Using enumerate() in python
- Using Numpy in python
- Using iterable without index in python
- Using general way via index
- Using the enumerate type function
- Using negative indexes in python
Method #1: Using For loop in python
Output:# Python3 code to iterate over a list in pythonlist = [1, 3, 5, 8, 9]# Using for loopfor n in list:print(n)
13589
Method #2: For loop and range() in python
In case we want to use the traditional For loop and range() in python which iterates from number x to number y.learn pandas in python# Python3 code to iterate over a list in pythonnum = [1, 3, 5, 8, 9]# getting length of list in pythonlength = len(num)# How to Iterating the indexfor i in range(length):print(list[i])
Output:
Method #3: Using while loop in python13589
Output:# Python3 code to iterate over a list in pythonnum = [1, 3, 5, 8, 9]# Getting length of list in pythonlength = len(num)i = 0# Iterating using while loop in pythonwhile i < length:print(num[i])i += 1
Method #4: Using list comprehension ways in python13589
Output:# Python3 code to iterate over a list in pythonnum = [1, 3, 5, 8, 9]# Using list comprehension in python[print(i) for i in num]
Method #5: Using enumerate() in python13589
suppose if we want to convert the list of set into an iterable list of tuples, then you can use the enumerate() function in python.
Output:# Python3 code to iterate over a list in pythonnum = [1, 3, 5, 8, 9]# Using enumerate() function in pythonfor i, val in enumerate(num):print (i, ",",val)
Method #6: Using Numpy in python0 , 11 , 32 , 53 , 84 , 9
If in code we provide n-dimensional lists, then it is sometimes better to use an external library (NumPy)
filter_none
Output:# Python program for using numpy func.# iterating over array in pythonimport numpy as geekz = z.reshape(3, 3)# iterating an array in pythonfor y in geek.nditer(z):print(y)
012345678
Method 7# − Using iterable without index in python
Example
sach_list = ['p','y','t','h','o','s','l','e','a','r','n','i','n','g']# Iterate over the list in pythonfor value in sach_list:print(value, end='')output:pythonslearning
Method 8# − Using general way via index
Example
sach_list = ['p','y','t','h','o','s','l','e','a','r','n','i','n','g']# Iterate over the list in pythonfor value in range(0,len(sach_list)):print(sach_list[value], end='')output:pythonslearning
Method 9# − Using the enumerate type function
Example
sach_list = ['p','y','t','h','o','s','l','e','a','r','n','i','n','g']# Iterate over the list in pythonfor value,char in enumerate(sach_list):print(char, end='')output:pythonslearning
Method 10# − Using negative indexes in python
Example
sach_list = ['p','y','t','h','o','s','l','e','a','r','n','i','n','g']output:pythonslearning
Summary:
In this article, we saw the 10 ways to iterate through a list in python, if any query about this section then please comment me.
If you want to learn linear regression in python then click on it.
Tags:
10 ways to iterate through a list in python
BEST OF LUCK!!!

No comments:
Post a Comment