;

Python Program to Add Two Lists


Tutorialsrack 28/04/2020 Python

In this Python program, we will learn how to add two lists in python. There are various ways to add two lists of elements in python. Here are some examples to perform the addition of two lists in python.

Here is the source code of the program to perform the addition of two lists.

Program 1: Python Program to Add Two Lists  - Simple Program

In this program, we simply run a loop to iterate and append the sum of the element to the new list. This is the simplest way to do the task.

Python Program to Add Two Lists  - Simple Program
# Python Program to Add Two Lists  - Simple Program

NumList1 = [12, 15, 25]
NumList2 = [15, 25, 35]
total = []
 
for j in range(3):
    total.append(NumList1[j] + NumList2[j])
 
print("\nThe total Sum of Two Lists =  ", total)
Output

The total Sum of Two Lists =   [27, 40, 60]

Program 2: Python Program to Add Two Lists Using For Loop and List Element entered by the User

In this program, we first take the input from the user and append those input elements to the list using the loop and that performing the addition of two lists.

Python Program to Add Two Lists Using For Loop and List Element entered by the User
# Python Program to Add two Lists Using For Loop and List Element entered by the User

NumList1 = []
NumList2 = []
total = []

# Take The Input From the User For Total No. of Elements in a List
Number = int(input("Enter the Total Number of List Elements: "))

# Take The Input From the User For List Elements of First and Second List
print("Enter the Items of a First List: ")
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList1.append(value)

print("\nEnter the Items of a Second List: ")
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList2.append(value)
    
for j in range(Number):
    total.append( NumList1[j] + NumList2[j])
 
print("\nThe total Sum of Two Lists = ", total)
Output

Enter the Total Number of List Elements: 3

Enter the Items of a First List: 

Enter the Value of 1 Element: 2

Enter the Value of 2 Element: 63

Enter the Value of 3 Element: 56

 

Enter the Items of a Second List: 

Enter the Value of 1 Element: 65

Enter the Value of 2 Element: 11

Enter the Value of 3 Element: 36

 

The total Sum of Two Lists =  [67, 74, 92]

Program 3: Python Program to Add Two Lists Using List Comprehension method

Python Program to Add Two Lists Using List Comprehension method
# Python Program to Add Two Lists Using List Comprehension method

# initializing lists 
NumList1 = [12, 15, 25]
NumList2 = [15, 25, 35]
total = []
  
# printing original lists 
print ("Original List 1 : " + str(NumList1)) 
print ("Original List 2 : " + str(NumList2)) 
  
# using list comprehension to add two list  
total = [NumList1[i] + NumList2[i] for i in range(len(NumList1))] 
  
# printing resultant list  
print ("Resultant list is : " + str(total))
Output

Original List 1 : [12, 15, 25]

Original List 2 : [15, 25, 35]

 

The total Sum of Two Lists = [27, 40, 60]

Program 4: Python Program to Add two Lists using map() with add operator

In this program, we can perform the addition of two lists using the map() function with add. We can pass two lists with add operators in the map() function to perform addition.

Python Program to Add two Lists using map() with add operator
# Python Program to Add two Lists using map() with add operator

# Import Module
from operator import add 
  
# initializing lists 
NumList1 = [13, 15, 25]
NumList2 = [15, 55, 25]
total = []
  
# printing original lists 
print ("Original list 1 : " + str(NumList1)) 
print ("Original list 2 : " + str(NumList2)) 
  
# using map() + add() to add two list  
total = list(map(add, NumList1, NumList2)) 
  
# printing resultant list  
print ("\nThe total Sum of Two Lists =  " + str(total))
Output

Original list 1 : [13, 15, 25]

Original list 2 : [15, 55, 25]

 

The total Sum of Two Lists =  [28, 70, 50]

Program 5: Python Program to Add two Lists using zip() function with sum() function

In this program, we will use the sum() function which is used to perform the index wise addition and that can be zipped by using the zip() function. This is also another way to perform the addition of two lists.

Python Program to Add two Lists using zip() function with sum() function
# Python Program to Add two Lists using zip() function with sum() function 

# Import Module
from operator import add 
  
# initializing lists 
NumList1 = [13, 15, 25]
NumList2 = [15, 55, 25]
total = []
  
# printing original lists 
print ("Original list 1 : " + str(NumList1)) 
print ("Original list 2 : " + str(NumList2)) 
  
# using zip() + sum() to add two list  
total = [sum(i) for i in zip(NumList1, NumList2)]

# printing resultant list  
print ("\nThe total Sum of Two Lists =  " + str(total)) 
Output

Original list 1 : [13, 15, 25]

Original list 2 : [15, 55, 25]

 

The total Sum of Two Lists =  [28, 70, 50]


Related Posts



Comments

Recent Posts
Tags