How to Python Append List to Another List

In this Python tutorial, we will learn about the “Python Append List to Another List” where we will understand how to appedn a list to another list in Python using the following set of examples.

  • How to append a list to another list in Python
  • How to append a list to another list using for loop in Python
  • How to append a list to another list flatten in Python
  • How to append a list to another list without brackets in Python

Python Append List to Another List

In this section, we will use different approaches or methods to append a list to another list.

How to append a list to another list in Python using extend() method

Lists in Python have a built-in function called extend() that adds an iterable to the end of the current iterable as an argument. When used for lists, it will add the list argument after the final item in the primary list.

The syntax is given below

list_name1.extend(list_name2)

Where elements from list_name2 are added to those from list_name1.

For example, create two lists, one list contains the name of the USA cities and the other contains the population of that city using the below code.

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

usa_populat = [8.5, 3.9, 2.7]

Append the second list “usa_populat” to the first list “usa_city” using the below code.

usa_city.extend(usa_populat)

Check the second appended list to the first list using the below code.

print(usa_city)
Python Append List to Another List Using Extend Function
Python Append List to Another List Using Extend Function

This is how to append one list to another list using the extend() of Python.

How to append a list to another list in Python using the concatenation (+) operator

Using Python’s list concatenation feature, the + operator, which permits adding several lists together, is another straightforward option. The output will be a single merged list in the order of the operands if we simply use the concatenation (+) operation to existing list variables.

Let’s see with an example how to append a list to another list using the concatenation (+) operator by following the below steps:

Create two lists, one list contains the name of the USA cities and the other contains the population of that city using the below code.

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

usa_populat = [8.5, 3.9, 2.7]

Use the concatenation (+) operator to add the list “usa_populat” to “usa_city” and store it in the new list using the below code.

usa_city_pop = usa_city + usa_populat

View the new list “usa_city_pop” using the below code.

print(usa_city_pop)
Python Append List to Another List Using Concatenation Operator
Python Append List to Another List Using Concatenation Operator

This is how to append one list to another list using the concatenation (+) operator of Python.

How to append a list to another list in Python using the chain() method

The itertools is a Python package that offers quick and effective utility techniques for iterables. The function chain() in this module receives a variable number of iterables of the same type and concatenates them according to the parameters in a predetermined order. The chain() function can be used to append multiple lists to create a single list.

Import the required libraries or methods using the below code.

import itertools

Create two lists, one list contains the name of the USA cities and the other contains the population of that city using the below code.

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

usa_populat = [8.5, 3.9, 2.7]

Use the chain() function to add the list “usa_populat” to “usa_city” and store it in the new list using the below code.

usa_city_pop = list(itertools.chain(usa_city,usa_populat))

Print the value of the new list “usa_city_pop” using the below code.

print(usa_city_pop)
Python Append List to Another List Using Chain Function
Python Append List to Another List Using Chain Function

This is how to append one list to another list using the chain() function of module itertools in Python.

How to append a list to another list in Python using the append() method

Create two lists, one list contains the name of the USA cities and the other contains the population of that city using the below code.

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

usa_populat = [8.5, 3.9, 2.7]

Use the append() function to add the list “usa_populat” to “usa_city” and store it in the new list using the below code.

usa_city.append(usa_populat)

View the added list to another list using the below code.

print(usa_city)
Python Append List to Another List Using Append Function
Python Append List to Another List Using Append Function

This is how to append one list to another list using the append() function of Python.

Read: Python check if the variable is an integer

Python Append List to Another List For Loop

Additionally, we may append every element from the second list to the first list using the list.append() function by iterating through the elements of the second list in a for loop.

Let’s take an example by following the below steps:

Using the code below, create two lists, one of which contains the names of US cities and the other of which has each city’s population.

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

usa_populat = [3.9, 8.5, 2.7]

Define the for loop and use the append() function within for loop to append the list “usa_populat” to another list “usa_city” using the below code.

for element in usa_populat:
  usa_city.append(element)

Check the appended list “usa_populate” to another list “usa_city” using the below code.

print(usa_city)
Python Append List to Another List For Loop
Python Append List to Another List For Loop

This is how to append one list to another list using the append() function within the loop of Python.

Read: ValueError: math domain error in Python

Python Append List to Another List Flatten

The Python data structure with the most flexibility is a list. As opposed to a 2D list, often known as a list of lists, which is a list object where each item is a list in and of itself.

When a list of lists is flattened, each item in the list of lists is unnested into a single list, for example changing [[2, 1, 3], [6, 5, 4], [7, 9, 8]] into [2, 1, 3, 6, 5, 4, 7, 9, 8].

Depending on the regularity and depth of the nested lists, flattening can be accomplished in Python via nested list comprehensions, for loops, built-in or recursion functions or by importing libraries.

In the section first, we will convert the 2d list to 1d and then append one list to another list by following the below steps:

First, create the function for converting the two-dimensional list to a one-dimensional using the below code.

def flatten_fun_list(_2dim_list):
    flatten_list = []
    # Repeat the outer list iteratively.
    for ele in _2dim_list:
        if type(ele) is list:
             # Iterate through the sublist if the element is one of the list types.
            for item in ele:
                flatten_list.append(item)
        else:
            flatten_list.append(ele)
    return flatten_list

Then create two 2d lists using the below code.

usa_state_2d = [["Albama"], ["Alaska"], ["Arizona"]]
usa_city_2d = [["Clanton"], ["Cordova"], ["Clifton"]]

print(type(usa_state_2d), usa_state_2d)
print(type(usa_city_2d), usa_city_2d)

Flatten the above two lists to a 1d list by passing them into the function “flatten_fun_list()” using the below code.

usa_state_1d = flatten_fun_list(usa_state_2d)
usa_city_1d  = flatten_fun_list(usa_city_2d)

print(type(usa_state_1d), usa_state_1d)
print(type(usa_city_1d), usa_city_1d)

Append the flattened list using the append() function.

usa_state_1d.append(usa_city_1d)

View the appended list using the below code.

print(usa_state_1d)
Python Append List to Another List Flatten
Python Append List to Another List Flatten

Read: Check if a number is a prime Python

Python Append List to Another List Without Brackets

If we look at the above subsection wherever the append() function is used to append one list to another list. The result is the list within another list, let’s take an example by following the below steps:

Create two different lists using the below code.

first_list = [1, 5, 7]
second_list = [2, 4, 8]

Append the second_list to the first_list using the append() function.

first_list.append(second_list)

View the appended list using the below code.

print(first_list)
Python Append List to Another List Without Brackets Example
Python Append List to Another List Without Brackets Example

In the above output, we can see “second_list” is appended to “first_list” as a separate list or with brackets as [1, 5, 7, [2, 4, 8]], but we want it as a single list of something [1, 5, 7, 2, 4, 8] which is “second_list” in “first_list” without brackets. To achieve this kind of list, we will use another function called extend().

Use the below code to append the “second_list” to the “first_list” without brackets using the extend() function.

first_list.extend(second_list)

Check the list using the below code.

print(first_list)
Python Append List to Another List Without Brackets
Python Append List to Another List Without Brackets

This is how to append one list to another list without brackets using the function extend() of Python Scipy.

You may also like to read the following Python tutorials.

At the end of the Python tutorial, we learned how to append a list to another list in Python. Additionally, we have also covered the following topics.

  • How to append a list to another list in Python
  • How to append a list to another list using for loop in Python
  • How to append a list to another list flatten in Python
  • How to append a list to another list without brackets in Python