Skip to main content

Write a Python program for performing list implementation with Basic Operating of LIST

Description

The Python code manages a list called "colleges" containing college names. It demonstrates list operations, including initialization, printing, appending, inserting, and removing elements from the list. The code illustrates the dynamic nature of lists, allowing easy modification of their content.

Code

program6a.py
colleges=["MLBP","BET","GPT","GIT"]
print(colleges)
colleges.append("BGS")
print(colleges)
colleges.insert(0,"Bhartesh")
print(colleges)
colleges.remove("GPT")
print(colleges)

Explanation of above code

  • List Initialization: The code initializes a list named 'colleges' with the inclusion of four college names: 'MLBP,' 'BET,' 'GPT,' and 'GIT.' Python's lists are versatile data structures, capable of storing elements of varying data types and being mutable, meaning their content can be changed.
  • Printing the Original List: In the initial stage, the code employs the 'print' function to display the original 'colleges' list, offering a clear illustration of its contents as initially defined. This serves as a baseline for future modifications.
  • Appending an Element: A pivotal step unfolds as the code utilizes the 'append' method, which is specific to lists. This method facilitates the straightforward addition of 'BGS' to the list, effectively appending this college name to the end. This showcases the dynamic nature of lists in Python.
  • Printing the List with the Appended Element: Following the successful addition of 'BGS,' the code reverts to the 'print' function, displaying the revised 'colleges' list. This visual representation serves to validate the integration of 'BGS,' confirming the update.
  • Inserting an Element at the Beginning: The code proceeds to employ the 'insert' method, a versatile list method. This method allows for the precise insertion of 'Bhartesh' at the commencement of the list, exerting control over both the index for insertion and the element itself. Such flexibility is a hallmark of Python lists.
  • Printing the List with the Inserted Element: In the aftermath of introducing 'Bhartesh' at the list's outset, the code once again invokes the 'print' function. This presentation of the 'colleges' list underscores the alteration, with 'Bhartesh' now residing at the list's inception.
  • Removing an Element: The code steps into a notable operation, utilizing the 'remove' method, an essential list method in Python. This method targets and eliminates 'GPT' from the list, finding and deleting its initial occurrence. This controlled removal operation effectively trims down the list's size.
  • Printing the List with the Removed Element: Following the calculated excision of 'GPT,' the code circles back to printing the 'colleges' list, affirming the absence of 'GPT' and underscoring the list's streamlined composition post-removal.

Learn more

Reference