How to Add Elements in List in Python Using For Loop [5 use cases]

We will use the for loop to add elements to a list with the help of the Python append() method. Also, we will know how to add the elements to the empty and non-empty lists throughout this tutorial by covering the following topics.

  • How to Add Elements in List in Python Using For Loop
  • How to Add Elements in Empty List in Python Using For Loop

How to Add Elements in List in Python Using For Loop

We are going to use the Python append() method within the loop to add elements to the pre-defined non-empty list.

The most straightforward method to add an item to the end of a list in Python is using the append() method. This method takes a single argument, the item we wish to add, and appends it to the list in Python.

Adding elements to a list in Python using a for loop combined with the append() function is a common and straightforward operation. This process allows us to iterate through an iterable (like a list, range, etc.), perform operations or apply conditions, and then add the results to a list in Python.

Case 1: How to add elements in a list from another list in Python using for loop

First, initialize the list which contains city names of the USA as a string using the below code.

usa_city = ["New York", "Los Angeles", "Chicago", "San Antonio"]

Create another list whose elements we will add to the above-created list “usa_city”.

append_usa_cty = ["Huston","Phoenix","Philadelphia"]

Define the for loop with the append() method to add the elements of “append_usa_cty” to “usa_city” list using the below code.

for i in range(len(append_usa_cty)):
  usa_city.append(append_usa_cty[i])

View the list “usa_city” to see the added elements from the list “append_usa_cty”.

print(usa_city)
How to Add Elements in List in Python Using For Loop Non-Empty List
How to Add Elements in List in Python Using For Loop Non-Empty List

This is how to add elements in a list from another list in Python using for loop with the append() function.

Case 2: How to add elements in a list from user input in Python using for loop

To add elements in list in Python using for loop with user input: First, initialize the list which contains city names of the USA as a string using the below code.

usa_city = ["New York","Los Angeles","Chicago","San Antonio"]

Define a Python variable which is the number of city names we want to enter. For example, we will enter the value 2 for the below-defined variable because we are going to add the two city names in the Python list

num_city = int(input("Number of cities"))

Define the for loop that will run till the number of city names we want to enter into the Python list or to the length of the variable “num_city”, each time loop will take an input or city name from the user using the input() and add it to the list “usa_city” using the append() method.

for i in range(0, num_city):
    city_name = str(input())
    usa_city.append(city_name)
    

View the added city name of the USA in the Python list using the below code.

print(usa_city)
How to Add Elements in List in Python Using For Loop Non-Empty List User Input
How to Add Elements in List in Python Using For Loop Non-Empty List User Input

Let’s understand what’s happening behind code numbers 11 and 12 in the above output:

  • num_city = int(input(“Number of cities: “)): The Python method input() accepts input from the user and this entered input which is 2 is converted into an integer because the method int(input()) is wrapped by the int() method in Python. And then the value 2 is assigned to the Python variable named num_city, Which means we want to add two city names to the list in Python.
  • for i in range(0, num_city): Then the for loop in Python begins and runs two times because the range of the loop is from 0 to 2 behind the scene.
  • Within the for loop there is code city_name = str(input()): This code accepts the city names and converts them into the string, then assign the string to the variable “city_name”. So we have entered two city names “Dallas” and “San Jose” one by one as the for loop each time iterates.
  • After, that there is another code in the for loop in Python is usa_city.append(city_name): The append() method adds the element to the list in Python. So we provided the Python variable “city_name” to this method append(city_name) for adding the two city names to the list “usa_city” one by one as the Python for loop each time iterates.

This is how to add the elements to the non-empty Python list from the user input or another list in Python.

Case 3: How to Add Elements in Empty List in Python Using For Loop

In the above subsection, we have learned how to add the elements in a non-empty list or a list that already contains some values in Python.

In this section, we will add the elements to the empty list, the procedure for adding the element to the empty list is the same as above, but here we will use the empty list instead of the non-empty list.

Case 4: How to add elements in an empty list from another list in Python using for loop

To add elements in list in Python using for loop from another Python list: First, initialize the empty list which is going to contain the city names of the USA as a string using the below code.

usa_city = []

Create another list whose elements we will add to the above-created empty list “usa_city”.

append_usa_cty = ["Huston","Phoenix","Philadelphia","New York","Los Angeles","Chicago","San Antonio"]

Define the for loop with the append() method to add the elements of “append_usa_cty” to the empty “usa_city” list using the below code.

for i in range(len(append_usa_cty)):
  usa_city.append(append_usa_cty[i])

View the list “usa_city” to see the added elements from the list “append_usa_cty”.

print(usa_city)
How to Add Elements in Empty List in Python Using For Loop
How to Add Elements in Empty List in Python Using For Loop

This is how we can add elements in list in Python using for loop from another Python list.

Case 5: How to add elements in an empty list from user input in Python using for loop

First, initialize the empty list which is going to contain the city names of the USA as a string using the below code.

usa_city = []

Create a variable for the number of city names to be entered. To add the three city names to the list “usa_city,” for instance, we will enter the value 3 for the variable below.

num_city = int(input("Number of cities"))

Define the loop that will run till the number of city names we want to enter into the list in Python or to the length of the variable “num_city”, each time loop will take an input or city name from the user using the input() and add it to the empty list “usa_city” using the append() method.

for i in range(0, num_city):
    city_name = str(input())
    usa_city.append(city_name)
    

View the added city name of the USA using the below code.

print(usa_city)
How to Add Elements in List in Python Using For Loop Empty List User Input
How to Add Elements in List in Python Using For Loop Empty List User Input

Let’s understand what’s happening behind code numbers 3 and 4 in the above output:

  • num_city = int(input(“Number of cities: “)): The method input() accepts input from the user and this entered input which is 3 is converted into an integer because the method int(input()) is wrapped by the method int(). And then the value 3 is assigned to the variable num_city, Which means we want to add three city names to the list.
  • for i in range(0, num_city): Then the loop begins and runs two times because the range of the loop is from 0 to 3 behind the scene.
  • Within the for loop there is code city_name = str(input()): This code accepts the city names and converts them into the string, then assign the string to the variable “city_name”. So we have entered three city names “New York”, “San Jose”, and “Dallas” one by one as the loop each time iterates.
  • After, that there is another code in the loop usa_city.append(city_name): The append() method add the element to the list. So we provided the variable “city_name” to this method append(city_name) for adding the two city names to the list “usa_city” one by one as the loop each time iterates.

This is how to add the elements to the empty list from the user input or another list in Python.

Conclusion

We have learned how to add the elements to a Python list from the user input or from another list. Also covered how to perform the addition of the elements to the empty or non-empty list by covering the following topics.

  • How to Add Elements in List in Python Using For Loop
  • How to Add Elements in Empty List in Python Using For Loop

You may like the following Python tutorial: