Skip to content

Create a new list in for loop Python | Example code

  • by

Using a for loop with append method you can create a new list in for loop Python.

Example creating a new list for each for loop in Python

Python simple example code creates and fills a list of lists in for a loop. This example will generate a new list from variables in a loop.

For each iteration in the loop multiplying the value by 10.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = []
for number in list1:
    calc = number * 10
    list2.append(calc)

print(list2)

Output:

Create a new list in for loop Python

Another example List comprehensions

It provides a more compact way to write this pattern.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

list2 = [number * 10 for number in list1]

print(list2)

Output:

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Do comment if you have any doubts and suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading