Python while iterate list

Python List While Loop

Python List is a collection of items. While loop can be used to execute a set of statements for each of the element in the list.

In this tutorial, we will learn how to use While loop to traverse through the elements of a given list.

Syntax of List While Loop

Elements of list are ordered. So, we can access the elements using index. We will make use of this property to traverse through items of list using while loop.

In general, the syntax to iterate over a list using while loop is

  • element contains value of the this element in the list. For each iteration, next element in the list is loaded into this variable with the changing index.
  • myList is the Python List over which we would like to traverse through.
  • statement(s) are block are set of statement(s) which execute for each of the element in the list.

Examples

1. Iterate over a list of strings using While loop

In this example, we will take a list of strings, and iterate over each string using while loop, and print the string length.

Python Program

myList = ['pineapple', 'banana', 'watermelon', 'mango'] index = 0 while index < len(myList): element = myList[index] print(len(element)) index += 1

Reference tutorials for the above program

2. Iterate over a list of numbers using While loop

In this example, we will take a list of numbers, and iterate over each number using while loop, and in the body of while loop, we will check if the number is even or odd.

Читайте также:  Сколько раз нужно кормить питона

Python Program

myList = [5, 7, 8, 3, 4, 2, 9] index = 0 while index < len(myList): element = myList[index] if element % 2 == 0: print('even') else: print('odd') index += 1
odd odd even odd even even odd

Reference tutorials for the above program

Summary

In this tutorial of Python Examples, we learned how to use While Loop to iterate over Python List elements.

Источник

How to Iterate (Loop) Over a List in Python

How to Iterate (Loop) Over a List in Python Cover Image

In this tutorial, you’ll learn how to iterate (or loop) over a list in Python. You’ll learn how to iterate with for loops, while loops, comprehensions, and more. What’s more, is that you’ll learn when each of these methods is the best method to use. Given that there are many different ways of accomplishing this, it can be helpful to understand which method to use.

Python lists are one of the cornerstone data structures in the language. They are ordered and indexable, meaning that their order matters and that you can access items based on their order. They’re also heterogeneous, meaning that they can hold many different data types. Because of their versatility, knowing how to work with them is an important skill for a Pythonista of any level.

By the end of this tutorial, you’ll have learned:

  • How to iterate over Python lists using for loops, while loops and comprehensions
  • How to enhance your iterating using the range() function and the enumerate() function
  • How to iterate over multiple lists in Python, and
  • How to iterate and manipulate lists in Python

A Guide to Iterating Over a List in Python

The table below breaks down the different methods in which you can loop over a list in Python. To learn more about each of these methods, keep reading for a deeper dive.

Источник

Оцените статью