6.1 Perform basic operations on the lists.

  • Write Python program to perform following operations on lists :
    a) Create
    b) Access
    c) Update
    d) Delete elements in list

In Python, a list is a versatile and mutable data structure used to store a collection of items. Lists can hold elements of different data types, such as integers, strings, and even other lists. They are one of the most commonly used data structures due to their flexibility and ease of use.

Key Characteristics of Lists:

  1. Ordered:
    • Lists maintain the order of the elements as they are added. The first element has an index of 0, the second 1, and so on.
  2. Mutable:
    • The contents of a list can be changed after it is created. You can add, remove, or modify elements.
  3. Heterogeneous:
    • A list can contain elements of different data types. For example, you can have a list with integers, strings, and floats all in the same list.
  4. Dynamic Size:
    • The size of a list can grow or shrink dynamically as you add or remove elements.
  5. Indexing and Slicing:
    • Lists support indexing, allowing access to individual elements. Negative indexing is also supported, where -1 refers to the last element. Slicing allows you to retrieve a sublist.
  6. Common Operations:
    • Creation: Lists are created using square brackets [].
    • Access: Elements are accessed using their index.
    • Update: Elements can be modified by assigning a new value to a specific index.
    • Delete: Elements can be removed using del, remove(), or pop() methods.
    • Appending: New elements can be added using append() or extend().
# a) Create a List
my_list = ["apple", "banana", "cherry", "date"]
print("Original List:", my_list)

# b) Access elements in a List
print("\nAccessing elements in the list:")
print("First element:", my_list[0])
print("Second element:", my_list[1])
print("Last element:", my_list[-1])

# c) Update elements in a List
print("\nUpdating elements in the list:")
my_list[1] = "blueberry"
print("Updated List:", my_list)

# d) Delete elements in a List
print("\nDeleting elements from the list:")
del my_list[2]  # Deleting the third element
print("List after deletion:", my_list)

# You can also remove elements using other methods:
# Remove by value
my_list.remove("apple")
print("List after removing 'apple':", my_list)

# Pop method removes the last element by default or a specific index
popped_element = my_list.pop()
print("Popped element:", popped_element)
print("List after popping last element:", my_list)

OUTPUT:
Original List: ['apple', 'banana', 'cherry', 'date']

Accessing elements in the list:
First element: apple
Second element: banana
Last element: date

Updating elements in the list:
Updated List: ['apple', 'blueberry', 'cherry', 'date']

Deleting elements from the list:
List after deletion: ['apple', 'blueberry', 'date']

List after removing 'apple': ['blueberry', 'date']
Popped element: date
List after popping last element: ['blueberry']

Explanation:

  • Create: A list is created using square brackets [].
  • Access: Elements are accessed using their index, with the first element at index 0.
  • Update: Elements can be updated by assigning a new value to a specific index.
  • Delete: Elements can be deleted using the del keyword, remove() method (by value), or pop() method (by index).
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top