Python Add Lists / Join / Concatenate Two or More Lists – My programming school


This tutorial covers the following topic – Python Add lists. It describes various ways to join/concatenate/add lists in Python. For example – simply appending elements of one list to the tail of the other in a for loop, or using +/* operators, list comprehension, extend(), and itertools.chain() methods.

Most of these techniques use built-in constructs in Python. However, the one, itertools.chain() is a method defined in the itertools module. You must also see which of these ways is more suitable in your scenario. After going through this post, you can evaluate their performance in case of large lists.

By the way, it will be useful if you have some elementary knowledge about the Python list. If not, please do go through the linked tutorial.

Python Add Lists – 10 Ways to Join/Concatenate Lists

For loop to add two lists

It is the most straightforward programming technique for adding two lists.

  • Traverse the second list using a for loop
  • Keep appending elements in the first list
  • The first list would expand dynamically

Finally, you’ll have a single list having all the items from other lists.

# Python Add lists example
# Sample code to add two lists using for loop

# Test input lists 
in_list1 = [21, 14, 35, 16, 55] 
in_list2 = [32, 25, 71, 24, 56] 

# Using for loop to add lists
for i in in_list2 : 
	in_list1.append(i) 

# Displaying final list 
print ("nResult: **********nConcatenated list using for loop: "
							+ str(in_list1))

python add lists using for loop

Plus (+) operator to merge two lists

Many languages use + operator for appending/merging strings. Python supports it also, and even for the lists.

It is a straightforward merge operation using the “+” operator. And you can easily merge lists by adding one to the back of the other.

# Python merge lists
# Sample code to merge two lists using + operator 

# Test input lists 
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]

# Apply + operator to merge lists
in_list3 = in_list1 + in_list2

# Displaying final list 
print ("nResult: **********nPython merge list using + operator: "
				+ str(in_list3))

python merge lists using plus operator

Mul (*) operator to join lists

It’s entirely a new method to join two or more lists and is available from Python 3.6. You can apply it to concatenate multiple lists and deliver one unified list.

# Python join two lists
# Sample code to join lists using * operator

# Test input lists 
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]

# Apply * operator to join lists
in_list3 = [*in_list1, *in_list2]

# Displaying final list 
print ("nResult: **********nPython join list using * operator: "
				+ str(in_list3))

python join lists using star operator

List comprehension to concatenate lists

List comprehension allows any operation (concatenation) on the input lists and can produce a new one without much of the effort.

This method processes the list like in a “for” loop, i.e., element by element.

# Python concatenate two lists
# Sample code to concatenate lists using list comprehension

# Test input lists 
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]

# Apply list comprehension to concatenate lists
in_list3 = [n for m in [in_list1, in_list2] for n in m]

# Displaying final list 
print ("nResult: **********nPython concatenate list using list comprehension: "
				+ str(in_list3))

python concatenate list using list comprehension

Built-in list extend() method

This function is a part of the Python list class and can also be used to add or merge two lists. It does an in-place expansion of the original list.

# Demonstrate Python Add lists
# Sample code to add two lists using list.extend()

# Test input lists 
in_list1 = [21, 14, 35, 16, 55] 
in_list2 = [32, 25, 71, 24, 56] 

# Using Python list.extend() method to add lists
in_list1.extend(in_list2)

# Displaying final list 
print ("nResult: **********nPython Add lists using list.extend(): "
							+ str(in_list1))

python add lists using list extend method

itertools.chain() to combine lists

Python itertools chain() function takes multiple iterables and produces a single list. The output is the concatenation of all input lists into one.

# Illustarte Python join lists
# Sample code to join lists using itertools.chain()
import itertools 

# Test input lists 
in_list1 = [21, 14, 35, 16, 55] 
in_list2 = [32, 25, 71, 24, 56] 

# Using itertools.chain() method to join lists
in_list3 = list(itertools.chain(in_list1, in_list2))

# Displaying final list 
print ("nResult: **********nPython join lists using itertools.chain(): "
							+ str(in_list3))

python join lists using itertools chain method

You’ve seen various methods to add/join/merge/concatenate lists in Python. Now, you should evaluate which one fits the most in your scenario. Also, you can now evaluate them based on their speed in case of large data sets. By the way, to learn Python from scratch to depth, do read our step by step Python tutorial.



Source link

Tags:

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.