×

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements.

It can contain any kind of data type inside it but in order to concatenate two lists, the lists must be of the similar data types. A user can have many lists in a code, which sometimes we might need to merge, join, or concatenate. Merging a list is very useful in many ways, and using this elaboration, we shall see the various ways by which we can concatenate two lists in Python.

In this article, we will learn how to add two lists in Python. Adding or Concatenating 2 lists in Python can be done in many ways such as:

  • Concatenate two lists in Python through append()
  • Concatenate two lists in Python using the + operator
  • Concatenate two lists in Python using the List comprehension
  • Using extend() method
  • Using * operator
  • Using itertools.chain()

1. Using append() function

Using this way, we first traverse the second list and then append all the elements in the first list one by one, so that the first list has all the elements added into it.

Example:

# concatenation using append()
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
fori in list2 :
	list1.append(i);
# Printing new list
print ("Concatenated list is : "+ str(list1))

Output:

Concatenated list is : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

2. Using + operator

The most convenient method to perform a list concatenation is the use of “+” operator. It can easily add the whole list behind the other list in one go and hence perform the concatenation.

Example:

# list addition using + operator
# Initialize lists
list3 = [1, 4, 5, 6, 5]
list4 = [3, 5, 7, 2, 5]


list3 = list3 + list4


# Printing concatenated list
print ("New list is : "+ str(list3))

Output:

New list is : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

3. By list Comprehension

List comprehension also can complete the task of list concatenation. In this case, a new list is created.

Example:

# adding list using list comprehension
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
new_list = [y for x in [list1, list2] for y in x]
# Printing new list
print ("new list using list comprehension is: "+ str(new_list))

Output:

new list using list comprehension is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

4. Using extend()

The extend() is the function used in lists by Python therefore can be used to perform this action. This function extends the first list by adding the second list to it.

Example:

# concatenation using extend()
# Initialize lists
list3 = [1, 4, 5, 6, 5]
list4 = [3, 5, 7, 2, 5]


list3.extend(list4)


# Printing new list
print ("New list using list.extend() is: "+ str(test_list3))

Output:

New list using list.extend() is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

5. Using * operator

The * operator is the new addition method for list concatenation and works only for Python 3.6+. This operator can concatenate any number of lists by returning a new list.

Example:

# adding lists using * operator
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]


new_list = [*list1, *list2]


# Printing new list
print ("new list using * operator is: "+ str(new_list))

Output:

new list using * operator is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

6. Using itertools.chain()

The itertools.chain() returns the iterable after chaining its arguments in one and hence does not require to store the concatenated list if only its initial iteration is required. This is useful when concatenated list has to be used just once.

Example:

# adding lists using itertools.chain()
importitertools
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]


# using itertools.chain() to concat
new_list = list(itertools.chain(list1, list2))


# Printing concatenated list
print ("new list using itertools.chain() : "+ str(new_list))

Output:

new list using itertools.chain() is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Related Topics

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

What does base case mean in recursion

Base cases and recursive stages are used to define recursive functions. In a primary subject, we compute the outcome right away, given the function call's arguments. In a recursive step, we...

4 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Make Notepad using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

5 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.