Python list join two lists

Python : Join / Merge lists ( two or more)

In this article we will discuss different ways to Merge / Join two or more lists in python.

Suppose we have two lists i.e.

# List of strings list1 = ["This" , "is", "a", "sample", "program"] # List of ints list2 = [10, 2, 45, 3, 5, 7, 8, 10]

We want to merge the contents of these two list into a single list i.e.

['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

There are different ways to do this. Let’s discuss them by one by one.

Frequently Asked:

Join / Merge two lists in python using + operator

In python, we can use the + operator to merge the contents of two lists into a new list. For example,

Читайте также:  Формат html в ворде

We can use + operator to merge two lists i.e.

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Merge two lists final_list = list_1 + list_2 print('Merged List:') print(final_list)
Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It returned a new concatenated lists, which contains the contents of both list_1 and list_2. Whereas, list_1 and list_2 remained same as original.

Join / Merge two lists in python using list.extend()

In the previous example, we created a new list containing the contents of the both the lists. But what if we want to extend any existing list? We can extend any existing list by concatenating the contents of any other lists to it using the extend() function of list i.e.

list.extend() makes a list longer by appending the elements of another list at the end of the calling list object. For example,

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Makes list1 longer by appending the elements of list2 at the end. list_1.extend(list_2) print('Merged List:') print(list_1)
Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It extended the list_1 by adding the contents of list_2 at the end of list_1.

Join / Merge two lists in python using unpacking

In python, we can unpack the contents on any iterable object using the * operator. So, *list will unpack the contents of a list. We can unpack the contents of both the lists and create a new list with the merged contents. For example,

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Merge two lists final_list = [*list_1, *list_2] print('Merged List:') print(final_list)
Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It unpacked the contents of both the lists and created a new list with the contents of both the lists.

Join / Merge two lists in python using itertools.chain()

In python, the itertools module provides a function chain() to merge the contents of multiple iterable sequences,

It creates a chain of all the iterable sequences passed as arguments and returns an iterator.

This iterator returns the elements from first iterable sequence until it is exhausted and then moves to the next iterable. We can use this iterator to created a merged list of contents. For example,

import itertools # List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Join two lists final_list=list(itertools.chain(list_1, list_2)) print('Merged List:') print(final_list)
Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

Join / Merge two lists in python using for loop

We can iterate over all elements of a list using for loop and during iteration we can append each element to an another list. This way we can extend the contents of a list. For example,

# List of strings list_1 = ["This" , "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Iterate over a list and add elements to another list for elem in list_2: list_1.append(elem) print('Extended List:') print(list_1)
Extended List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

We iterated over all the elements in list_2 and while iteration added each element at the end of list_1. Therefore list_1 is now extended and contains the contents of both the lists i.e. original list_1 and list_2.

Join / Merge multiple lists using + operator

We can merge the contents of multiple lists to a new list using the + operator. For example,

list_1 = ["This" , "is", "a", "sample", "program"] list_2 = [10, 2, 45, 3, 5, 7, 8, 10] list_3 = [11, 12, 13] # Merge 3 lists into a single list merged_list = list_1 + list_2 + list_3 print('Merged List:') print(merged_list)
Merged List: ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]

We learned about different ways to join or merge multiple lists in python.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Python Join Two Lists

There are several ways to join, or concatenate, two or more lists in Python.

One of the easiest ways are by using the + operator.

Example

list1 = [«a», «b» , «c»]list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

Another way to join two lists are by appending all the items from list2 into list1, one by one:

Example

list1 = [«a», «b» , «c»]list2 = [1, 2, 3]

for x in list2:
list1.append(x)

Or you can use the extend() method, which purpose is to add elements from one list to another list:

Example

Use the extend() method to add list2 at the end of list1:

list1 = [«a», «b» , «c»]list2 = [1, 2, 3]

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Join Two Lists Python – Learn Joining Two Lists With Examples

Hello everyone, in Join Two Lists Python tutorial, we will learn how to join two lists in python. Already, we know list is an important data structure of python. Joining two lists in python is very easy. So let’s see how to do them. But before proceeding further check the previous tutorial that explain you how to create a list in python and everything related to lists.

Join Two Lists Python Tutorial – An Easy Way To Join Lists

Meaning of Joining Lists

  • Joining lists means, we combine the items of two lists into a list.
  • It is also called merging or concatenation of lists.

Ways Of Joining Lists

In python, there is two ways by using that you can join two lists.

Using + Operator

Let’s take an example, where lists are as follows –

  • Here we have taken two lists.
  • Then we have joined these two lists and stored the result into an another list.

Join Two Lists Python

Now, if you don’t want to store the joined lists into a separate list then what will have you to do, let’s see.

So the result is as follows.

Join Two Lists Python

Using extend() method

Now we will see how to join two lists using extend() method. So understanding this we are taking an example. So let’s write the code for this.

  • Using extend() method, we can’t store the concatenation of two lists into a third list.
  • We can store the result only in an existing list.

Now see the output of this code.

Join Two Lists Python

Here you can see List_3 has no items because using extend() method, we can’t store the concatenation of two lists into a third list.

Taking Inputs From Users

Now we will see an example where we will take inputs from users.

So, for this we have to write the below code.

What We Did ?

  • First of all we have initialized an empty list.
  • Then we have asked the user to enter size of their list1.
  • We have cast it to integer so user can only enter integers.
  • Now we will need to ask the user to enter those numbers so for this we have started a for loop.
  • Next, we have asked the user to enter items into list1.
  • And then these numbers into list.
  • We have repeated same procedure for second list.
  • Then we have joined these two lists using + operator.
  • And last simply printed the concatenated list.

And now, the output of this example is –

Join Two Lists Python

Dealing With Strings

Till now we have seen only examples of integers items of a list. And now we will see how to concatenate two lists if they contain string items. So for implementing this concept we have to write the following code.

Join Two Lists Python

  • If we are joining two lists that contain string then we have to the follow same process as we do in the case of integers.

So that’s all for this Join Two Lists Python tutorial . You can leave your queries below in comment section to have a discussion about that. And please share this tutorial with your friends. Thank You.

Leave a Comment Cancel reply

subscribe to simplified codings youtube channel

Belal Khan

Hi my name is Belal Khan.
I am the creator of this blog. By profession I am a software engineer and I love to share my knowledge over the internet. You can read more about me here.

About

If you just started learning Python then this blog is for you. When I started learning about Python; I though I should create a blog to share my Python Knowledge, and hence I’ve created Simplified Python.
Click Here if you want to know the man behind Simplified Coding.

Источник

Python — Join Lists

There are several ways to join, or concatenate, two or more lists in Python.

One of the easiest ways are by using the + operator.

Example

list3 = list1 + list2
print(list3)

Another way to join two lists is by appending all the items from list2 into list1, one by one:

Example

list1 = [«a», «b» , «c»]list2 = [1, 2, 3]

for x in list2:
list1.append(x)

Or you can use the extend() method, where the purpose is to add elements from one list to another list:

Example

Use the extend() method to add list2 at the end of list1:

list1 = [«a», «b» , «c»]list2 = [1, 2, 3]

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Оцените статью