VOOZH about

URL: https://www.geeksforgeeks.org/python/add-items-to-list-while-iterating-python/

⇱ Add items to List While Iterating - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Add items to List While Iterating - Python

Last Updated : 23 Jul, 2025

Python has many ways to append to a list while iterating. List comprehension is a compact and efficient way to append elements while iterating. We can use it to build a new list from an existing one or generate new values based on a condition.


Output
[1, 2, 3, 4, 5, 6]

Other ways to append to a list while iterating are:

Using extend() Method

If we want to append multiple items to a list at once, we can use the extend() method. This method adds all elements from another list to the original list.


Output
[1, 2, 3, 4, 5, 6]

Using a While Loop

Sometimes, a while loop might be useful for appending to a list. You can manually control the loop’s conditions.


Output
[1, 2, 3, 4, 5, 6]

Using map() Function

The map() function applies a function to all items in an iterable. This method can also be used to append items while iterating.


Output
[1, 2, 3, 4, 5, 6]
Comment