Python multiple lists to one

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,

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.

Источник

Ways to concatenate multiple lists in Python

Ways To Concatenate Multiple Lists In Pythn

In this article, we will understand various techniques to concatenate multiple lists in Python. Python lists provide us a way to store data and perform manipulations on it.

Techniques to concatenate multiple lists in Python

Either of the following techniques can be used to concatenate two or more lists altogether:

  • By using itertools module
  • By using Python ‘+’ operator
  • By using Python ‘*’ operator

1. Using Python itertools.chain() method

Python itertools module provides us with itertools.chain() method to concatenate multiple lists together.

The itertools.chain() method accepts data of different iterables such as lists, string, tuples, etc and provides a linear sequence of elements out of them.

This function works irrespective of the data type of the input data.

itertools.chain(list1, list2, . listN)
import itertools x = [10, 30, 50, 70] y = [12, 16, 17, 18] z = [52, 43, 65, 98] opt = list(itertools.chain(x,y,z)) print ("Concatenated list:\n",str(opt))
Concatenated list: [10, 30, 50, 70, 12, 16, 17, 18, 52, 43, 65, 98]

2. Using Python ‘*’ operator

Python ‘*’ operator provides a much efficient way to perform manipulation on the input lists and concatenate them together.

It represents and unwraps the data elements at their provided index position.

[*input_list1, *input_list2, . *inout_listN]

As mentioned, the *input_list1, *input_list2, etc would contain elements within that list at the given index in the mentioned order.

x = [10, 30, 50, 70] y = [12, 16, 17, 18] z = [52, 43, 65, 98] opt = [*x, *y, *z] print ("Concatenated list:\n",str(opt))
Concatenated list: [10, 30, 50, 70, 12, 16, 17, 18, 52, 43, 65, 98]

3. Using Python “+” operator

Python ‘+’ operator can be used to concatenate the lists together.

x = [10, 30, 50, 70] y = [12, 16, 17, 18] z = [52, 43, 65, 98] opt = x+y+z print ("Concatenated list:\n",str(opt))
Concatenated list: [10, 30, 50, 70, 12, 16, 17, 18, 52, 43, 65, 98]

Conclusion

Thus, in this article, we have unveiled different ways to concatenate multiple lists in Python.

References

Ways to concatenate list in Python

Источник

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