- Add two Lists element wise in Python
- Add two lists element-wise using zip() function
- Frequently Asked:
- Add two lists element-wise using map() function
- Add two lists element-wise using NumPy
- Add two lists element wise using numpy.add()
- Related posts:
- Python — Add List Items
- Example
- Insert Items
- Example
- Extend List
- Example
- Add Any Iterable
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- How To add Elements to a List in Python
- Prerequisites
- append()
- insert()
- extend()
- List Concatenation
- Conclusion
Add two Lists element wise in Python
This article will discuss different ways to add two lists element-wise in Python.
Table Of Contents
Add two lists element-wise using zip() function
Python provides a function zip(), which takes multiple iterables as arguments and returns an aggregated iterable of tuples. The nth tuple of the iterable contains the nth element from each passed iterable argument. We can use this to aggregate two lists element-wise. The steps are as follows,
- Pass two lists as arguments in the zip() function. It returns an iterable of tuples.
- Iterate over this iterable of tuples.
- Call the sum() function for each tuple during iteration and add returned value to the new list.
first = [11, 12, 13, 14, 15, 16] second = [71, 77, 89, 51, 90, 59] # Add two lists element-wise using zip() & sum() final_list = [sum(value) for value in zip(first, second)] print(final_list)
Frequently Asked:
It added both the lists element-wise and returned a new list.
How did it work?
We passed the two list objects first and second to the zip() function. It aggregated both the lists and returned an iterable of tuples. The contents of this iterable tuple are like,
(11, 71) (12, 77) (13, 89) (14, 51) (15, 90) (16, 59)
Related Articles
Then, call the sum() function for each tuple and add the result to a new list. In the end, this new list will contain the sum of the first and second list objects element-wise.
Add two lists element-wise using map() function
Python provides a function map(). It takes a callback function and one or more iterables (lists, tuples or sets etc.) as arguments. i.e.
Then it iterates over all the given iterables simultaneously. Each iteration picks an item from each iterable and passes it to the callback function (first argument of map() function). Values returned by the callback function are stored in an object of map class. This map object can be converted into a sequential data structure to get all the returned values.
So, to add two lists element-wise, we will go following arguments to the map() function,
- A lambda function, which accepts two arguments and returns a sum of those values.
- Both the lists object.
The map() function will iterate over both lists together. Like, in the first iteration it will pick the values 11 and 71. Then it will pass those values to the lambda function (first argument), which will return the sum of passed values i.e. 88. The map() function will append this value in a map object and will go forward with the second iteration. In the end, we will convert this map object to a list. This list will contain the sum of our two original lists element-wise. For example,
first = [11, 12, 13, 14, 15, 16] second = [71, 77, 89, 51, 90, 59] # Add two lists element-wise using zip() & Lambda function final_list = list(map(lambda a,b: a+b, first, second)) print(final_list)
It added both the lists element-wise and returned a new list.
Add two lists element-wise using NumPy
We can also convert both the lists to NumPy arrays and then use the + operator. It will add the NumPy arrays’ contents element-wise and return a new NumPy Array. Then we can convert back this NumPy array to a list. For example,
import numpy as np first = [11, 12, 13, 14, 15, 16] second = [71, 77, 89, 51, 90, 59] # Add two lists element-wise using numpy final_list = list( np.array(first) + np.array(second)) print(final_list)
It added both the lists element-wise and returned a new list.
Add two lists element wise using numpy.add()
The NumPy array provides a function add(), which takes two sequences as arguments and add these sequences element-wise. We can pass out two lists in this add() function, and it will add them element-wise. For example,
import numpy as np first = [11, 12, 13, 14, 15, 16] second = [71, 77, 89, 51, 90, 59] # Add two lists element-wise using numpy.add() final_list = np.add(first,second).tolist() print(final_list)
It added both the lists element-wise and returned a new list.
We learned about different ways to add two lists element-wise in Python.
Related posts:
Python — Add List Items
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
Insert Items
To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:
Example
Insert an item as the second position:
Note: As a result of the examples above, the lists will now contain 4 items.
Extend List
To append elements from another list to the current list, use the extend() method.
Example
Add the elements of tropical to thislist :
thislist = [«apple», «banana», «cherry»]
tropical = [«mango», «pineapple», «papaya»]
thislist.extend(tropical)
print(thislist)
The elements will be added to the end of the list.
Add Any Iterable
The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.).
Example
Add elements of a tuple to a list:
thislist = [«apple», «banana», «cherry»]
thistuple = («kiwi», «orange»)
thislist.extend(thistuple)
print(thislist)
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
How To add Elements to a List in Python
In this tutorial, we will learn different ways to add elements to a list in Python.
There are four methods to add elements to a List in Python.
- append() : append the element to the end of the list.
- insert() : inserts the element before the given index.
- extend() : extends the list by appending elements from the iterable.
- List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.
Prerequisites
In order to complete this tutorial, you will need:
This tutorial was tested with Python 3.9.6.
append()
This function adds a single element to the end of the list.
fruit_list = ["Apple", "Banana"] print(f'Current Fruits List fruit_list>') new_fruit = input("Please enter a fruit name:\n") fruit_list.append(new_fruit) print(f'Updated Fruits List fruit_list>')
Current Fruits List ['Apple', 'Banana'] Please enter a fruit name: Orange Updated Fruits List ['Apple', 'Banana', 'Orange']
This example added Orange to the end of the list.
insert()
This function adds an element at the given index of the list.
num_list = [1, 2, 3, 4, 5] print(f'Current Numbers List num_list>') num = int(input("Please enter a number to add to list:\n")) index = int(input(f'Please enter the index between 0 and len(num_list) - 1> to add the number:\n')) num_list.insert(index, num) print(f'Updated Numbers List num_list>')
Current Numbers List [1, 2, 3, 4, 5] Please enter a number to add to list: 20 Please enter the index between 0 and 4 to add the number: 2 Updated Numbers List [1, 2, 20, 3, 4, 5]
This example added 20 at the index of 2 . 20 has been inserted into the list at this index.
extend()
This function adds iterable elements to the list.
extend_list = [] extend_list.extend([1, 2]) # extending list elements print(extend_list) extend_list.extend((3, 4)) # extending tuple elements print(extend_list) extend_list.extend("ABC") # extending string elements print(extend_list)
[1, 2] [1, 2, 3, 4] [1, 2, 3, 4, 'A', 'B', 'C']
This example added a list of [1, 2] . Then it added a tuple of (3, 4) . And then it added a string of ABC .
List Concatenation
If you have to concatenate multiple lists, you can use the + operator. This will create a new list, and the original lists will remain unchanged.
evens = [2, 4, 6] odds = [1, 3, 5] nums = odds + evens print(nums) # [1, 3, 5, 2, 4, 6]
This example added the list of evens to the end of the list of odds . The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.
Conclusion
Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator
Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!