Python Program to Append, Delete and Display Elements of a List Using Classes

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Object-Oriented Programming(OOPS):

Object-Oriented Programming (OOP) is a programming paradigm based on the concepts of classes and objects. It is used to organize a software program into simple, reusable code blueprints (typically referred to as classes), which are then used to build individual instances of objects. Object-oriented programming languages include JavaScript, C++, Java, and Python, among others.

A class is a generic blueprint that can be used to generate more specific, concrete things. Classes are frequently used to represent broad groups, such as Cars or Dogs, that share property. These classes indicate what properties, such as color, an instance of this type will have, but not the value of those attributes for a specific object.

An object comprises data, such as the raw or preprocessed materials at each step of an assembly line, as well as behavior, such as the action performed by each assembly line component.

Python, like other general-purpose programming languages, has always been an object-oriented language. It enables us to create a program with an Object-Oriented approach. Classes and objects are simple to build and utilize in Python.

The task is to perform append, delete and display elements of the given list using classes in Python.

Program to Append, Delete and Display Elements of a List Using Classes

Below is the full approach to perform append, delete and display elements of the given list using classes in Python.

Approach:

  • Create a class and use a default constructor to initialize its values(initializing an empty list).
  • Initialize empty list using [] operator or list() function.
  • Create three methods inside the class.
  • AddEle: Which appends the given argument to the list using the append function.
  • RemEle: Which removes the given argument from the list using the remove() function.
  • DisplayList: This prints all the elements of the given list.
  • Implement methods for appending, deleting, and displaying list elements, and return the corresponding values.
  • Create an object to represent the class.
  •  Using the object, call the appropriate function based on the user’s selection.
  •  Print the modified list after every operation
  • The Exit of the Program.

Below is the implementation:

# creating a class named ListOperations
class ListOperations():
  # Default constructor to initialize its values(initializing an empty list).
    def __init__(self):
      # initializing empty list using [] operator or list() function.
        self.givenllist = []
    # Creating three methods inside the class.
    # AddEle: Which appends the given argument to the list using the append function.

    def AddEle(self, eleme):
        return self.givenllist.append(eleme)
    # RemEle: Which removes the given argument from the list using the remove() function.

    def RemEle(self, eleme):
        self.givenllist.remove(eleme)
    # DisplayList: This prints all the elements of the given list.
    # Implement methods for appending, deleting, and displaying list elements,
    # and return the corresponding values.

    def DisplayList(self):
        return (self.givenllist)


# Create an object to represent the class.
listObject = ListOperations()
# taking a choice from the user .
# initializing option as 1
option = 1
while option != 0:
    print("Enter [0] to Exit the program")
    print("Enter [1] to Add element to the given list")
    print("Enter [2] to remove element from the given list")
    print("Enter [3] to print all the elements of the given list")
    option = int(input("Enter some random choice from 0 to 3 = "))
    if option == 1:
      # scanning the element as user input using
      # int(input()) and storing it in a variable
        ele = int(
            input("Enter some random number to be appended to the given list = "))
        listObject.AddEle(ele)
        print("The given list after adding element=",
              ele, '\n', listObject.DisplayList())

    elif option == 2:
        ele = int(
            input("Enter some number which should be removed from the given list = "))
        listObject.RemEle(ele)
        print("The given list after removing the given element=",
              ele, '\n', listObject.DisplayList())

    elif option == 3:
        print("The given list : \n", listObject.DisplayList())
    elif option == 0:
        print("Exit of program")
    else:
        print("The choice entered by user is invalid")

    print()

Output:

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 19
The given list after adding element= 19 
[19]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 11
The given list after adding element= 11 
[19, 11]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 35
The given list after adding element= 35 
[19, 11, 35]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 38
The given list after adding element= 38 
[19, 11, 35, 38]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 75
The given list after adding element= 75 
[19, 11, 35, 38, 75]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 585
The given list after adding element= 585 
[19, 11, 35, 38, 75, 585]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 685
The given list after adding element= 685 
[19, 11, 35, 38, 75, 585, 685]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 191
The given list after adding element= 191 
[19, 11, 35, 38, 75, 585, 685, 191]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 1
Enter some random number to be appended to the given list = 29
The given list after adding element= 29 
[19, 11, 35, 38, 75, 585, 685, 191, 29]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 2
Enter some number which should be removed from the given list = 191
The given list after removing the given element= 191 
[19, 11, 35, 38, 75, 585, 685, 29]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 3
The given list : 
[19, 11, 35, 38, 75, 585, 685, 29]

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 8
The choice entered by user is invalid

Enter [0] to Exit the program
Enter [1] to Add element to the given list
Enter [2] to remove element from the given list
Enter [3] to print all the elements of the given list
Enter some random choice from 0 to 3 = 0
Exit of program

Explanation:

  • The ListOperationsclass is created, and the __init__() function is used to initialize the class’s values.
  • The methods for adding, deleting, and displaying list elements have been specified.
  • The menu is printed, and the user makes his or her selection.
  • The class’s object is created.
  • The object is used to call the appropriate method based on the user’s selection.
  • The final list has been printed.

Related Programs: