The sum() function is used to add two lists using the index number of the list elements grouped by the zip() function. A zip() function is used in the sum() function to group list elements using index-wise lists.,In Python, a map() function is used to add two lists bypassing the list variables (lt1, lt2) and add as parameters. Inside the map() function, an added parameter acts like an additional operator to add lists and returns the sum.,Let's consider a program to add the list elements using the zip function and sum function in Python.,It is a simple method that adds the two lists in Python using the loop and appends method to add the sum of lists into the third list. A for loop performs the addition of both lists with the same index number and continuously iterates the elements until the end of the list. After that append method, insert the added elements to the third list.
Display the List1['Rose', 'Lotus', 24, 'Gold', 'USA']
Display the List2[1, 2, 4, 5, 6]
Display the Department List['Web Designing', 40, 20]
Display the CS Department[58, 'Ms Wiley']
Python Original list 1: [5, 10, 15, 20, 25, 30] Python Original list 2: [2, 4, 6, 8, 10, 12] Addition of the list lt1 and lt2 is: [7, 14, 21, 28, 35, 42]
Python list 1: [2, 4, 6, 8, 10, 30] Python list 2: [2, 4, 6, 8, 10, 12] Addition of the list lt1 and lt2 is: [4, 8, 12, 16, 20, 42]
Display the elements of List 1[4, 8, 12, 16, 20, 24]
Display the elements of List 2[2, 4, 6, 8, 10, 12]
Sum of the list 1 and list 2 is: [6, 12, 18, 24, 30, 36]
Enter the total number of the list elements: 5 Enter the items into the List 1: Enter the value of 1 index is: 3 Enter the value of 2 index is: 6 Enter the value of 3 index is: 9 Enter the value of 4 index is: 12 Enter the value of 5 index is: 15 Enter the items into the List 2: Enter the value of 1 index is: 2 Enter the value of 2 index is: 4 Enter the value of 3 index is: 6 Enter the value of 4 index is: 8 Enter the value of 5 index is: 10 The addition of the two list is[5, 10, 15, 20, 25]
Display the elements of List 1[6, 12, 18, 3, 6, 9]
Display the elements of List 2[4, 8, 12, 2, 4, 6]
Sum of the list 1 and list 2 is: [10, 20, 30, 5, 10, 15]
Last Updated : 16 Aug, 2021,GATE CS 2021 Syllabus
Output:
The original list 1 is: [1, 3, 5, 5, 4] The original list 2 is: [4, 6, 2, 8, 10] The original list 3 is: [7, 5, 2, 9, 11] The extended and modified list is: [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]
listA = ['Mon', 'Tue', 'Wed'] listB = ['2 pm', '11 am', '1 pm'] listC = [1, 3, 6] # Given lists print("Given list A: ", listA) print("Given list B: ", listB) print("Given list C: ", listC) # using + operator res_list = listA + listB + listC # printing result print("Combined list is : ", res_list)
Running the above code gives us the following result −
Given list A: ['Mon', 'Tue', 'Wed']
Given list B: ['2 pm', '11 am', '1 pm']
Given list C: [1, 3, 6]
Combined list is: ['Mon', 'Tue', 'Wed', '2 pm', '11 am', '1 pm', 1, 3, 6]
listA = ['Mon', 'Tue', 'Wed'] listB = ['2 pm', '11 am', '1 pm'] listC = [1, 3, 6] # Given lists print("Given list A: ", listA) print("Given list B: ", listB) print("Given list C: ", listC) # using zip res_list = list(zip(listA, listB, listC)) # printing result print("Combined list is : ", res_list)
Combining strings or characters in programming is called concatenation. In Python, concatenation can be performed on numbers, strings and elements of a list. For example, you can add a string “Hey” and another string “there!” to form a new string “Hey there!”. You can also add two numbers such as 2 and 5 to get a result 7. The most common form of concatenation is by using the + operator.,Concatenating or merging two or multiple lists is a common operation of a programmer. In this tutorial, we will concatenate lists with and without duplicate elements.,extend() function is also used to concatenate lists, it simply adds the whole list at the end of the first list.,Concatenation is one of the easiest ways to combine list elements or strings while codding in Python. But while using the chains() method, import the itertools module using the import statement. Additionally, you can also use methods such as join() to concatenate two strings.
Example:
# Python program to merge lists # Using + Operator # Initializing lists list1 = [2, 3, 4, 2, 2] list2 = [4, 5, 6, 7, 34, 56] list3 = [1, 5, 33, 2, 34, 46] # merge lists using + Operator newlist = list1 + list2 + list3 # Print output print('Merged List: ', newlist)
Output:
Merged List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 1, 5, 33, 2, 34, 46]
Example:
# Python program to concatenate lists # Using extend function # Initializing lists list1 = [2, 3, 4, 2, 2] list2 = [4, 5, 6, 7, 34, 56] list3 = [4, 67, 2, 2, 4, 66] # concatenate lists using extend() list1.extend(list2) list1.extend(list3) # Print output print('Concatenated List: ', list1)
Output
Concatenated List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 1, 5, 33, 2, 34, 46]
Single Line Code Example:
# Python program to concatenate lists # Initializing lists list1 = [2, 3, 4, 2, 2] list2 = [4, 5, 6, 7, 34, 56] list3 = [1, 5, 33, 2, 34, 46] # Concatenate lists newlist = [y for x in [list1, list2, list3] for y in x ] # Print output print(Concatenated List: ',newlist)
In the previous example, we created a new list containing the contents of the both the lists. But what if we want to extend any existing list? We can extend any existing list by concatenating the contents of any other lists to it using the extend() function of list i.e.,In python, we can use the + operator to merge the contents of two lists into a new list. For example, ,It unpacked the contents of both the lists and created a new list with the contents of both the lists.,In python, we can unpack the contents on any iterable object using the * operator. So, *list will unpack the contents of a list. We can unpack the contents of both the lists and create a new list with the merged contents. For example,
Suppose we have two lists i.e.
# List of strings list1 = ["This", "is", "a", "sample", "program"] # List of ints list2 = [10, 2, 45, 3, 5, 7, 8, 10]
We want to merge the contents of these two list into a single list i.e.
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]
We can use + operator to merge two lists i.e.
# List of strings list_1 = ["This", "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Merge two lists final_list = list_1 + list_2 print('Merged List:') print(final_list)
In the previous example, we created a new list containing the contents of the both the lists. But what if we want to extend any existing list? We can extend any existing list by concatenating the contents of any other lists to it using the extend() function of list i.e.
list.extend(anotherList)
list.extend() makes a list longer by appending the elements of another list at the end of the calling list object. For example,
# List of strings list_1 = ["This", "is", "a", "sample", "program"] # List of ints list_2 = [10, 2, 45, 3, 5, 7, 8, 10] # Makes list1 longer by appending the elements of list2 at the end. list_1.extend(list_2) print('Merged List:') print(list_1)
1. Take in the number of elements for the first list and store it in a variable. 2. Take in the elements of the list one by one. 3. Similarly, take in the elements for the second list also. 4. Merge both the lists using the ‘+’ operator and then sort the list. 5. Display the elements in the sorted list. 6. Exit.,1. User must enter the number of elements for the first list and store it in a variable. 2. User must then enter the elements of the list one by one using a for loop and store it in a list. 3. User must similarly enter the elements of the second list one by one. 4. The ‘+’ operator is then used to merge both the lists. 5. The sort function then sorts the list in ascending order. 6. The sorted list is then printed.,The program takes two lists, merges them and sorts the merged list.,Python Program to Sort a List According to the Second Element in Sublist
a = []
c = []
n1 = int(input("Enter number of elements:"))
for i in range(1, n1 + 1):
b = int(input("Enter element:"))
a.append(b)
n2 = int(input("Enter number of elements:"))
for i in range(1, n2 + 1):
d = int(input("Enter element:"))
c.append(d)
new = a + c
new.sort()
print("Sorted list is:", new)
Case 1: Enter number of elements: 5 Enter element: 10 Enter element: 20 Enter element: 35 Enter element: 55 Enter element: 71 Enter number of elements: 2 Enter element: 5 Enter element: 37 Sorted list is: [5, 10, 20, 35, 37, 55, 71] Case 2: Enter number of elements: 3 Enter element: 2 Enter element: 4 Enter element: 8 Enter number of elements: 3 Enter element: 2 Enter element: 4 Enter element: 0 Sorted list is: [0, 2, 2, 4, 4, 8]
Using the zip_longest() method of itertools module, you can iterate through two parallel lists at the same time. The method lets the loop run until the longest list stops.,Using zip() method, you can iterate through two lists parallel as shown above.,In this example, you will learn to iterate through two lists in parallel.,The loop runs until the shorter list stops (unless other conditions are passed).
Example 1: Using zip (Python 3+)
list_1 = [1, 2, 3, 4]
list_2 = ['a', 'b', 'c']
for i, j in zip(list_1, list_2):
print(i, j)
Output
1 a
2 b
3 c
Example 2: Using itertools (Python 2+)
import itertools list_1 = [1, 2, 3, 4] list_2 = ['a', 'b', 'c'] # loop until the short loop stops for i, j in zip(list_1, list_2): print(i, j) print("\n") # loop until the longer list stops for i, j in itertools.zip_longest(list_1, list_2): print(i, j)