Iterate Python List - While Loop - Tutorial Kart
[Pages:3]Python List While Loop
Python List While Loop
To iterate over elements of a Python List using While Loop statement, start with index of zero and increment the index till the last element of the list using length of the list.
In this tutorial, we will go through example Python programs, that demonstrate how to iterate a list using while loop in Python.
Example 1 ? Iterate Python List using While Loop
In this example, we will take a Python List, and iterate over all the elements of this list using while loop.
Python Program
list_1 = [4, 52, 6, 9, 21] index = 0 while index < len(list_1) :
print(list_1[index]) index += 1
Output
4 52 6 9 21
Example 2 ? Iterate Python List in Reverse using While Loop
In this example, we will take a Python List, and iterate over all the elements from end to start of this list using while loop.
Python Program
list_1 = [4, 52, 6, 9, 21] index = len(list_1) - 1 while index >= 0 :
print(list_1[index]) index -= 1
Output
21 9 6 52 4
Example 3 ? Iterate & Update Python List using While Loop
In this example, we will take a Python List on numbers, iterate over all the elements and increment each element by 2.
Python Program
list_1 = [4, 52, 6, 9, 21]
#update list items index = 0 while index < len(list_1) :
list_1[index] += 2 index += 1
#print list items index = 0 while index < len(list_1) :
print(list_1[index]) index += 1
Output
6 54 8 11 23
Conclusion
In this Python Tutorial, we learned how to iterate over elements of a Python List using While Loop statement.
Python Programming
Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments
Control Statements
Python If Python If Else Python While Loop Python For Loop
Python String
Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings
Functions
Python Functions Python Builtin Functions
Python Collections
Python List Python Dictionary
Advanced
Python Multithreading
Useful Resources
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- dictionaries store connections between pieces of list
- reading and writing vector data with ogr
- how to code in python 3 digitalocean
- chimera programmer s guide
- 7 iteration — how to think like a computer scientist
- iterate python list while loop tutorial kart
- python programming an introduction to computer science
- python programing an introduction to computer science
Related searches
- powershell do while loop examples
- powershell while loop array
- while loop powershell
- while loop counter python
- python code to loop through column
- for while loop python
- python while loop user input
- python while loop multiple conditions
- while loop with input python
- python while loop boolean
- python list in list insert
- python how to loop through a dictionary