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: Adding Elements of Two Lists in Python
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

Adding Elements of Two Lists in Python

Last updated: Sep 24, 2023 10:47 pm
By Meenakshi Agarwal
Share
7 Min Read
python add two list elements- 4 ways
python add two list elements- 4 ways
SHARE

This tutorial describes four unique ways to add elements of two lists in Python. For example – using a for loop to iterate the lists, add corresponding elements, and store their sum at the same index in a new list. Some of the other methods you can use are using map() and zip() methods.

Contents
For loop to add elements of two listsList comprehension to add elements of two listsmap() and add() functions to add given listszip() and sum() functions to add given lists

All of these procedures use built-in functions in Python. However, while using the map(), you’ll require the add() method, and zip() will need to be used with the sum() function. Both these routines are defined in the operator module, so you would have to import them into your program. After finishing up this post, you can assess which of these ways is more suitable for your scenario.

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 Two List  Elements – 4 Unique Ways

For loop to add elements of two lists

It is the simplest approach to add elements of two lists. In this method, Python for loop is used to iterate the smaller of the two lists. In every iteration, add the corresponding values at the running index from the two lists, and insert the sum in a new list.

# Python add two list elements using for loop

# Setting up lists 
in_list1 = [11, 21, 34, 12, 31, 26] 
in_list2 = [23, 25, 54, 24, 20] 

# Display input lists 
print ("\nTest Input: **********\n Input List (1) : " + str(in_list1)) 
print (" Input List (2) : " + str(in_list2)) 

# Using for loop
# Add corresponding elements of two lists
final_list = [] 

# Choose the smaller list to iterate
list_to_iterate = len(in_list1) < len(in_list2) and in_list1 or in_list2

for i in range(0, len(list_to_iterate)): 
	final_list.append(in_list1[i] + in_list2[i]) 

# printing resultant list 
print ("\nTest Result: **********\n Smaller list is : " + str(list_to_iterate)) 
print (" Resultant list is : " + str(final_list))

You can see the addition of elements of two lists in Python from the below example.

python add two list elements using for loop

List comprehension to add elements of two lists

Python list comprehension is a unique shorthand technique in Python to create lists at runtime. It lets the programmer perform any operation on the input list elements.

This method is also a bit faster for manipulating the list data structure. Check out the below sample Python program.

# Python add two list elements using list comprehension

# Setting up lists 
in_list1 = [11, 21, 34, 12, 31] 
in_list2 = [23, 25, 54, 24, 20, 27] 

# Display input lists 
print ("\nTest Input: **********\n Input List (1) : " + str(in_list1)) 
print (" Input List (2) : " + str(in_list2)) 

# Using list comprehension
# Add corresponding elements of two lists
final_list = [] 

# Choose the smaller list to iterate
list_to_iterate = len(in_list1) < len(in_list2) and in_list1 or in_list2

final_list = [in_list1[i] + in_list2[i] for i in range(len(list_to_iterate))]  

# printing resultant list 
print ("\nTest Result: **********\n Smaller list is : " + str(list_to_iterate)) 
print (" Resultant list is : " + str(final_list))

This program will produce the following result:

python add two list elements using list comprehension

map() and add() functions to add given lists

Python map() is one of the higher-order functions in Python. It takes another method as its first argument, along with the two lists.

Both the input lists are passed to the input function (add() in our case) as parameters. This function adds up the elements of two lists and returns a Python iterable as output.

We convert the iterable into a list using the list constructor method. Check out the complete example below:

# Python add two list elements using map() and add()
from operator import add 

# Setting up lists 
in_list1 = [11, 21, 34, 12, 31] 
in_list2 = [23, 25, 54, 24, 20, 27] 

# Display input lists 
print ("\nTest Input: **********\n Input List (1) : " + str(in_list1)) 
print (" Input List (2) : " + str(in_list2)) 

# Using map() and add()
# Add corresponding elements of two lists
final_list = list(map(add, in_list1, in_list2))  

# printing resultant list 
print ("\nTest Result: **********\n Resultant list is : " + str(final_list))

This example will give the following outcome:

map() and add() functions to add given lists

zip() and sum() functions to add given lists

The sum() function adds list elements one by one using the index. And Python zip() function groups the two list items together.

This approach is also an elegant way to add elements of two lists in Python. Check out the full example with the source code below:

# Python add two list elements using zip() and sum()

# Setting up lists 
in_list1 = [11, 21, 34, 12, 31, 77] 
in_list2 = [23, 25, 54, 24, 20] 

# Display input lists 
print ("\nTest Input: **********\n Input List (1) : " + str(in_list1)) 
print (" Input List (2) : " + str(in_list2)) 

# Using zip() and sum()
# Add corresponding elements of two lists
final_list = [sum(i) for i in zip(in_list1, in_list2)]  

# printing resultant list 
print ("\nTest Result: **********\n Resultant list is : " + str(final_list))

The above code will provide the following output:

zip() and sum() functions to add given lists

You’ve seen various methods here for Python Add two list elements. Now, you can choose any of them based on your business case. 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

How to Use Extent Report in Python

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 python add lists using plus, extend, for loop, etc methods Python Add Lists
Next Article python zip - builtin python function Python Zip

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