Write A Python Function To Append A List To The Second List

Hi in this article you will learn a Python function that takes a list as input arguments,  appends the first list to the second list.  so the second list will contain the  appended version of the first list.

Let's try to use the  built-in function named as extend()  and  append the first list to the second list.

 

 

Write A Python Function To Append A List To The Second List

 

 

 

Write A Python Function To Append A List To The Second List

Define the Python function named as add_two_list()  pass the two input arguments to this function,  both the input arguments are lists.  now using the built in function, take the  second list and use the extent function call to append the  first list to it.

def add_two_list(list1, list2):
   list2.extend(list1)





In the main program we need to ask the user to enter a list and accept the to list as input to the Python  program.

Take the length variable and use the input function call as the user to enter first list length,  take an empty list and using the for loop  ask the user to enter the list items.  Now  append all the items to the list to  fill the new empty list.

length = int(input("Enter List1 Length : "))
first = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   first.append(temp)





Use the same method as mentioned above for getting the second list into the program,  use the length variable,  and an empty variable of the second list,  iterate using the for loop and accept the user input then frame the second list.

length = int(input("Enter List2 Length : "))
second = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   second.append(temp)





Now in the main program call the function that we had defined above.  pass the two  as input to the Python function.  Once the Python function gets executed the first list will be appointed to the second list,  use the print function call to print the second list.

add_two_list(first, second)
print(second)



Complete Python Program
===============================

def add_two_list(list1, list2):
   list2.extend(list1)


length = int(input("Enter List1 Length : "))
first = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   first.append(temp)


length = int(input("Enter List2 Length : "))
second = []
for _ in range(length):
   temp = int(input("Enter List Item : "))
   second.append(temp)

add_two_list(first, second)

print(second)





Output 1
======================

Enter List1 Length : 3
Enter List Item : 1
Enter List Item : 2
Enter List Item : 3
Enter List2 Length : 3
Enter List Item : 7
Enter List Item : 8
Enter List Item : 9
[7, 8, 9, 1, 2, 3]





Output 2
======================

Enter List1 Length : 2
Enter List Item : 45
Enter List Item : 67
Enter List2 Length : 5
Enter List Item : 1
Enter List Item : 2
Enter List Item : 3
Enter List Item : 4
Enter List Item : 5
[1, 2, 3, 4, 5, 45, 67]




Above Python program can also be modified to iterate all the items present in the  first list and append it to the  second list using the for loop,  this method is shown below.

def add_two_list(list1, list2):
   for item in list1:
       list2.append(item)



Just replace the above Python function to use the normal method to append the list items.

Output
================

Enter List1 Length : 2
Enter List Item : 33
Enter List Item : 44
Enter List2 Length : 3
Enter List Item : 11
Enter List Item : 22
Enter List Item : 00
[11, 22, 0, 33, 44]



Conclusion
===================

In the above python program we are accepting two lists as input to the Python program,  appending the first list to the second list, then printing the second list.


Execute the above Python program by providing random list inputs to the script.  the program will append the first list to the second list note down the second list.

Comment it down below if you have any queries regarding the above Python function.




Post a Comment

0 Comments