Skip to content

How to add elements in a list in Python using for loop

  • by

Use the list append() method to add elements in a list in Python using a for loop. The list.append function does not return any value (but None), it just adds the value to the list you are using to call that method.

Add elements in a list in Python using for loop

A simple example code first gets the length of the list obj. Then iterate over the list and Call list.append(object) within the loop to add object to list.

a_list = ["a", "b", "c"]
list_length = len(a_list)

for i in range(list_length):
    a_list.append("New")

print(a_list)

Output:

How to add elements in a list in Python using for loop

Output:

Another example adding a list into a list using a loop

r2 = []
r_1 = {'G':32,'H':3}
for i in r_1:
    r2.append([i, i+"I"])
print(r2)

Comment if you have any doubts or 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