Adding elements to the list

  • Python provides append() function which is used to add an element to the list. However, the append() function can only add value to the end of the list.

Program

#Declaring the empty list  
l =[]

#Number of elements will be entered by the user    
n = int(input("Enter the number of elements in the list:"))
 
# for loop to take the input  
for i in range(0,n):     
    # The input is taken from the user and added to the list as the item  
    l.append(input("Enter the item:"))     
print("printing the list items..")
   
# traversal loop to print the list items    
for i in l:   
    print(i, end = "  ")  


Output:

Enter the number of elements in the list:5
Enter the item:50
Enter the item:90
Enter the item:67
Enter the item:34
Enter the item:65
printing the list items..
50  90  67  34  65