TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Add Lists
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Python Add Lists

Last updated: Sep 23, 2023 10:19 pm
By Meenakshi Agarwal
Share
6 Min Read
python add lists using plus, extend, for loop, etc methods
python add lists using plus, extend, for loop, etc methods
SHARE

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.

Contents
For loop to add two listsPlus (+) operator to merge two listsMul (*) operator to join listsList comprehension to concatenate listsBuilt-in list extend() methoditertools.chain() to combine lists

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 for your scenario. After going through this post, you can evaluate their performance in the case of large lists.

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

Python Add Lists – 6 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 the + operator for appending/merging strings. Python supports it also, and even for 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 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. Let’s illustrate Python join lists with the help of a simple program.

# 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.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

Selenium Python Extent Report Guide

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Convert Python String to Int & Int to String with Examples Python String to Int and Back to String
Next Article python add two list elements- 4 ways Adding Elements of Two Lists in Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x