×

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

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Application to get live USD/INR rate 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...

4 minutes read.

Python IDE

What is an IDE?  An IDE or an Integrated Development Environment is a type of software where developers can develop many software’s and applications and run the programs inside it. An...

11 minutes read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python vs HTML

Python and HTML are not comparable since they are two separate categories of programming languages. Building the structures and layouts of a web page or app requires the usage of HTML,...

8 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Infinity

Introduction We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be...

5 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.