Write a Python Program to Create Append And Remove Lists in Python


Creating a list in Python,  appending a list,  and then removing an item from the list.  These are some of the operations that can be performed over a Python list.  

Python list is a mutable type, It means the items present in the list can be changed dynamically.

 

 

Write a Python Program to Create Append And Remove Lists in Python

 


Let's Write a Python Program to Create Append And Remove Lists in Python

Using the print  function call print a statement,  creating a list of integers. Take an integer variable and ask a user to enter the size of the list using input function call.

Take an empty list and initialize it to null. Using the for loop  to iterate over n items of list and ask user to enter the list item then append the item into an empty list. Finally once the for loop is completed  print the contents of the list using print function.

print("Lets create a List ... ")
n = int(input("Enter Size of List : "))
my_list = []
for _ in range(n):
   temp = int(input("Enter List Item : "))
   my_list.append(temp)
print("\nContents of List is : ", my_list)




Now using the friend function print appending the list,  ask a user to enter the list item to append to the list. Then using the find function call append the item into the list. Finally print the contents of the list.

print("Lets append List ...")
item = int(input("Enter List Item to append : "))
my_list.append(item) the
print("\nContents of List is : ", my_list)





Use the print function to print removing a list item. Ask the user to  enter an item that should be removed from the list. then using the list remove function removes the item present in the list.

Use try block to remove the item from the list. if the item is not present catch the exception value error and print the appropriate message.

print("Lets remove List item ...")
item = int(input("Enter List Item to be removed : "))
try:
   my_list.remove(item)
except ValueError:
   print("Item is not present in nor present in List")
print("Contents of List is : ", my_list)




Final Program
===================

print("Lets create a List ... ")
n = int(input("Enter Size of List : "))
my_list = []
for _ in range(n):
   temp = int(input("Enter List Item : "))
   my_list.append(temp)
print("\nContents of List is : ", my_list)

print("Lets append List ...")
item = int(input("Enter List Item to append : "))
my_list.append(item)
print("\nContents of List is : ", my_list)

print("Lets remove List item ...")
item = int(input("Enter List Item to be removed : "))
try:
   my_list.remove(item)
except ValueError:
   print("Item is not present in nor present in List")
print("Contents of List is : ", my_list)





OUTPUT
===================

Let's create a List ...
Enter Size of List : 3
Enter List Item : 1
Enter List Item : 2
Enter List Item : 3

Contents of List is :  [1, 2, 3]
Lets append List ...
Enter List Item to append : 4

Contents of List is :  [1, 2, 3, 4]
Let's remove List items ...
Enter List Item to be removed : 2
Contents of List is :  [1, 3, 4]



OUTPUT
===============

Let's create a List ...
Enter Size of List : 2
Enter List Item : 11
Enter List Item : 22

Contents of List is :  [11, 22]
Lets append List ...
Enter List Item to append : 33

Contents of List is :  [11, 22, 33]
Let's remove List items ...
Enter List Item to be removed : 44
Item is not present in nor present in List
Contents of List is :  [11, 22, 33]




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

Run the program and provide the input values then create a list, append the list item to the list of integers and then finally remove the list item.




Post a Comment

0 Comments