Iterate over a list in Python

  • List is like arrays in other languages, with the extra advantage of being dynamic in size. In Python, the list may be a sort of container in Data Structures, which is used to store multiple data at the same time. Unlike Sets, lists in Python are ordered and have a definite count.
  • There are multiple ways to iterate over a list in Python. Let’s see all the various ways to iterate over a list in Python, and performance comparison between them.

Method #1: Using For loop

[pastacode lang=”python” manual=”%23%20Python3%20code%20to%20iterate%20over%20a%20list%0AList%20%3D%20%5B1%2C%203%2C%205%2C%207%2C%209%5D%0A%20%20%0A%23%20Using%20for%20loop%0AFor%20i%20in%20list%3A%0A%20%20%20%20Print(i)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”1%0A3%0A5%0A7%0A9%0A” message=”” highlight=”” provider=”manual”/]

Method #2: For loop and range ()

In case we would like to use the traditional for loop which iterates from number x to number y.

[pastacode lang=”python” manual=”%23%20Python3%20code%20to%20iterate%20over%20a%20list%0AList%20%3D%20%5B1%2C%203%2C%205%2C%207%2C%209%5D%0A%20%20%0A%23%20getting%20length%20of%20list%0ALength%20%3D%20len(list)%0A%20%20%0A%23%20Iterating%20the%20index%0A%23%20same%20as%20’for%20i%20in%20range(len(list))’%0AFor%20i%20in%20range(length)%3A%0A%20%20%20%20Print(list%5Bi%5D)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”1%0A3%0A5%0A7%0A9%0A” message=”” highlight=”” provider=”manual”/]

Iterating using the index isn’t recommended if we will iterate over the elements (as done in Method #1).

Method #3: Using while loop

[pastacode lang=”python” manual=”%23%20Python3%20code%20to%20iterate%20over%20a%20list%0AList%20%3D%20%5B1%2C%203%2C%205%2C%207%2C%209%5D%0A%20%20%0A%23%20Getting%20length%20of%20list%0ALength%20%3D%20len(list)%0AI%20%3D%200%0A%20%20%0A%23%20Iterating%20using%20while%20loop%0AWhile%20i%20%3C%20length%3A%0A%20%20%20%20Print(list%5Bi%5D)%0A%20%20%20%20I%20%2B%3D%201%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”1%0A3%0A5%0A7%0A9%0A” message=”” highlight=”” provider=”manual”/]

Method #4: Using list comprehension (Possibly the most concrete way).

[pastacode lang=”python” manual=”%23%20Python3%20code%20to%20iterate%20over%20a%20list%0AList%20%3D%20%5B1%2C%203%2C%205%2C%207%2C%209%5D%0A%20%20%0A%23%20Using%20list%20comprehension%0A%5Bprint(i)%20for%20i%20in%20list%5D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”1%0A3%0A5%0A7%0A9%0A” message=”” highlight=”” provider=”manual”/]

Method #5: Using enumerate ()

If we would like to convert the list into an iterable list of tuples (or get the index supported a condition check, for example in linear search you would possibly need to save the index of minimum element), you’ll use the enumerate() function.

[pastacode lang=”python” manual=”%23%20Python3%20code%20to%20iterate%20over%20a%20list%0AList%20%3D%20%5B1%2C%203%2C%205%2C%207%2C%209%5D%0A%20%20%0A%23%20Using%20enumerate()%20%0AFor%20i%2C%20val%20in%20enumerate(list)%3A%0A%20%20%20%20Print%20(i%2C%20%22%2C%22%2Cval)%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”0%2C%201%0A1%2C%203%0A2%2C%205%0A3%2C%207%0A4%2C%209%0A” message=”” highlight=”” provider=”manual”/]

Note: Even method #2 is used to find the index, but method #1 can’t (Unless an additional variable is incremented every iteration) and method #5 gives a concise representation of this indexing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Read More

Robot Framework in Python

Definition Robot Framework is an open-source, keyword-driven test automation framework that uses Python for writing and executing automated…

Why you choose Python ?

Definition Python is a versatile, high-level programming language known for its simplicity, readability, and broad applicability across various…
Read More

Data Types in Python

Definition Data types in Python are classifications that specify the type of value a variable holds, determining what…