Python Append List to List Without Nesting

In this Python tutorial, you will learn how to append a list to another list without nesting in multiple ways with practical examples and realistic scenarios.

While working on a Python Project, I needed to merge two different columns from the database stored as a list in Python. And it should not be a nested list.

I found 4 different solutions in Python to append a list to a list without nesting.

4 Ways to Append List to List Without Nesting

Let me explain the scenario with one example so you can use the approach correctly.

These are two different lists,

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

Both lists have some data, so we need to append list_2 to list_1 without adding the brackets.

Let’s understand all the methods and approaches one by one with practical examples and different scenarios.

Python Append List to List Without Nesting Using for loop and append() Method

First, we will use the for loop and append() method to merge two lists in Python. So, the for loop will individually target all the second list elements.

The append() method helps to add the elements to the list.

Syntax of append() method in Python

list_1.appned(list_2)
  • list_1.appned(element): This syntax is perfect for our requirement
READ:  Python Copy Dict Without One Key

Let’s combine the for loop and append() method to merge two lists in Python.

today_sales = [2000, 4200,5000,6000,1500]
yesterday_sales = [1000, 3000, 4500, 6000, 1200]

for i in yesterday_sales:
    today_sales.append(i)

print("Total sales list: ", today_sales)
Python Append List to List Without Nesting Using for loop and append() Method

In the above code, we have two lists of today’s and yesterday’s sales. We need to merge both lists.

So, we initialized a for loop to iterate ” i “ in the second list of elements like this:
for i in yesterday_sales:, we use the append() method to add an element into the first list like today_sales.append(i).

How to Merge a List to Another List in Python Using While Loop and Insert() Method

We can also use the while loop and insert() method to append a list into another in Python.

The while loop targets the index positions of all the elements individually, and the insert() method adds an element based on the given index position.

Syntax

list.insert(index_position, element)

Let’s understand how we can merge both the list using the while loop and insert().

today_sale = [2000, 4200, 5000, 6000, 1500]
yesterday_sale = [1000, 3000, 4500, 6000, 1200]

i = 0
while i<len(yesterday_sale):
    today_sale.insert(len(today_sale), yesterday_sale[i])
    i+=1

print(today_sale)
How to merge a list to another list in Python Using while loop and insert() Method

We’ve initialized i = 0 to target elements from the 0 index in the above code. Then, while loop will iterate till len(yesterday_sale) like this: while i<len(yesterday_sale):

Then, using the insert() method like this today_sale.insert(len(today_sale), yesterday_sale[i]) to add an element at the end position of the list.

Append List to List Python Without Brackets Using extend() Method

In Python, extend() is the direct way to merge two lists without including brackets. This is a preferable approach to use in this type of scenario.

READ:  Write a Program to Find a Perfect Number in Python

Syntax

list_1.extend(list_2)
  • list_1.extend(list_2): Using the extend() method is more beneficial because it takes the list as a parameter, which means there is no need to iterate over single elements to merge the list.

Let’s understand how to use the extend() method to merge two lists in Python.

east_states = ['New York', 'Florida', 'Georgia']
west_states = ['California', 'Washington', 'Nevada']

east_states.extend(west_states)
print(east_states)
Append List to List Python Without Brackets Using extend() method

In the above code, we have two lists, east_states and west_states, and we need to merge both lists. So, we use the extend() method like this: east_states.extend(west_states).

Merge Two Lists in Python by Performing Concatenation using the (+=) Operator

Performing concatenation in Python is also a direct way to merge two lists. Generally, we use the ( + ) operator for concatenation. But here, we are using the (+=) operator because we need to change the original list.

Syntax for list concatenation in Python.

list_1 += list_2

Let’s understand how we can perform concatenation in Python to merge two lists without including the brackets.

east_states = ['New York', 'Florida', 'Georgia']
west_states = ['California', 'Washington', 'Nevada']
east_states += west_states
print(east_states)
Merge two lists in Python by performing concatenation using (+=) operator

In the above code, we are using += to append west_states into the east_states like this: east_states += west_states

Conclusion

In this Python article, you learned how Python appends a list to list without nesting in different ways and methods, such as using a for loop with the append() method, a while loop with the insert() method, an extend() method, and concatenation to merge two lists.

You may like to read: