Python – Combine list with other list elements

PythonServer Side ProgrammingProgramming

When it is required to combine list with other list elements, a simple iteration and ‘append’ method is used.

Below is a demonstration of the same −

Example

 Live Demo

my_list_1 = [12, 14, 25, 36, 15]

print("The first list is :")
print(my_list_1)

my_list_2 = [23, 15, 47, 12, 25]

print("The second list is :")
print(my_list_2)

for element in my_list_2 :
   my_list_1.append(element)

print ("The result is :")
print(my_list_1)

Output

The first list is :
[12, 14, 25, 36, 15]
The second list is :
[23, 15, 47, 12, 25]
The result is :
[12, 14, 25, 36, 15, 23, 15, 47, 12, 25]

Explanation

  • Two lists are defined and displayed on the console.

  • The second list is iterated over, and each element of second list is appended to the first list.

  • This is the output that is displayed on the console.

raja
Published on 06-Sep-2021 08:54:29
Advertisements