 # How To Add Elements To a List in a Loop in Python

You want to add elements to an existing list in Python. Using a loop is a great way that most Python developers choose. In this article, we will teach you how to add elements to a list in a loop in Python.

## How to add elements to a list in a loop in Python

Here is how you add elements to a list in a loop in Python.

### Add numbers in ascending order to a list

The loop in Python helps you add numbers in ascending order to a list. You will also need to use the method .append() to push an element to the end of an existing list. Here is the syntax:

[list] = []
[variable] = [lowest number]
for [variable] in range([integer]):
[list].append([variable])

As you see, the job is to create an empty list. The [variable] has the integer value that starts from the [lowest number] to the [integer] value you describe in the range(). The loop adds numbers in ascending order to the list.

Here is the code sample:

# Create an empty list
lstNumber = []

# Create an integer number starting from 0
number = 0

# The loop adds numbers from 0 to 5 to the list
for number in range(6):
lstNumber.append(number)

# Print the list out
print(lstNumber)

The output will be:

[0, 1, 2, 3, 4, 5]

### Add numbers in descending order to a list

You can also add numbers in descending order to the list. Here is the syntax:

[list] = []
[variable] = [highest integer]
for [variable] in range([variable], [lowest number], [step]):
[list].append([variable])

The [variable] will not start from 0. Instead, it starts from the integer that you assign. The for loop takes 3 parameters, with the [variable] as the highest number, [lowest number], and the [step] (in descending order).

See the code sample below:

# Create an empty list
lstNumber = []

# Create an integer number starting from 8
number = 8

# The loop adds numbers from 8 to 0 to the list
for number in range(number, -1, -1):
lstNumber.append(number)

# Print the list out
print(lstNumber)

The output will be:

[8, 7, 6, 5, 4, 3, 2, 1, 0]

### Add elements from one list to another list

You have 2 separate lists. The for loop helps you add all elements in a list into the other one. You still need to rely on the .append() method to push elements to the end of the list. The syntax is like this:

[list1] = [elements]
[list2] = [elements]
for element in [list2]:
[list1].append(element)

The for loop adds each element from the elements of the [list2] to the [list1]. The [list1] after being changed will contain elements from both [list1] and [list2]. You can see the code below:

# Create a List Fruit 1
lstFruit1 = ["banana", "pineapple", "lime"]
print("List Fruit 1:", lstFruit1)

# Create a List Fruit 2
lstFruit2 = ["orange", "apple"]
print("List Fruit 2:", lstFruit2)

# Use for loop to add elements from List Fruit 2 to List Fruit 1
for element in lstFruit2:
lstFruit1.append(element)

print("List Fruit 1 after being changed:", lstFruit1)

The output will be:

List Fruit 1: ['banana', 'pineapple', 'lime']
List Fruit 2: ['orange', 'apple']
List Fruit 1 after being changed: ['banana', 'pineapple', 'lime', 'orange', 'apple']

## Summary

To summarize, we have taught you how to add elements to a list in a loop in Python. You can add numbers to a list in order, or move elements from one list to the other list. All the work is done using Python’s built-in for loop.