Instagram
youtube
Facebook
Twitter

Python Program to Insert an Element at the End of a List

A Python Program to Insert an Element at the End of a List?

Code Explanation

Function Definition:
A function insert_at_end() is defined to add a new element at the end of the list.

Using append():
lst.append(element) adds the given element to the end of the list.

Input List & Element:
A sample list [1, 2, 3, 4] and element 5 are used for testing.

Function Call & Output:
The function is called, and the updated list [1, 2, 3, 4, 5] is printed.

 

Program:

def insert_at_end(lst, element):

    lst.append(element)

    return lst

my_list = [1, 2, 3, 4]

element = 5

updated_list = insert_at_end(my_list, element)

print("Updated list:", updated_list)