Python Lists

Lists are ordered, mutable collections of items. They are one of the most commonly used data types in Python for storing sequences of elements. Lists can contain items of different data types.


Creating Lists

You can create a list by placing comma-separated values inside square brackets.

Explanation: Lists can store multiple items in a single variable. They can include different data types such as strings, integers, floats, and even other lists.

# Creating a list of strings
fruits = ["apple", "banana", "cherry"]

# Creating a list of integers
numbers = [1, 2, 3, 4, 5]

# Creating a list with mixed data types
mixed_list = [1, "Hello", 3.14, True]




Accessing List Items

You can access individual items in a list using indexing. Python uses zero-based indexing.

Explanation: The index 0 accesses the first item in the list, while the index -1 accesses the last item.

# Accessing list items
print(fruits[0])  # Output: apple
print(fruits[-1]) # Output: cherry




Modifying List Items

Lists are mutable, meaning you can change their items after creation.

Explanation: You can modify items by accessing them using their index.

# Modifying list items
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']




Adding Items to a List

You can add items to a list using the append() method, insert() method, or the extend() method.

  • append() adds an item to the end of the list.
  • insert() adds an item at a specified position.
  • extend() adds multiple items to the end of the list.
# Adding items to a list
fruits.append("orange")
fruits.insert(1, "kiwi")
more_fruits = ["mango", "pineapple"]
fruits.extend(more_fruits)
print(fruits)  # Output: ['apple', 'kiwi', 'blueberry', 'cherry', 'orange', 'mango', 'pineapple']




Removing Items from a List

You can remove items from a list using the remove() method, pop() method, or the del statement.

  • remove() removes the first occurrence of a specified value.
  • pop() removes an item at a specified index and returns it.
  • del statement removes an item at a specified index.
# Removing items from a list
fruits.remove("kiwi")
print(fruits)  # Output: ['apple', 'blueberry', 'cherry', 'orange', 'mango', 'pineapple']

removed_fruit = fruits.pop(1)
print(removed_fruit)  # Output: blueberry
print(fruits)  # Output: ['apple', 'cherry', 'orange', 'mango', 'pineapple']

del fruits[0]
print(fruits)  # Output: ['cherry', 'orange', 'mango', 'pineapple']




List Methods

Python provides several built-in methods for list manipulation.

  • len(): Returns the number of items in a list.
  • sort(): Sorts the items in a list in ascending order.
  • reverse(): Reverses the order of items in a list.
# List methods
print(len(fruits))  # Output: 4

fruits.sort()
print(fruits)  # Output: ['cherry', 'mango', 'orange', 'pineapple']

fruits.reverse()
print(fruits)  # Output: ['pineapple', 'orange', 'mango', 'cherry']




Python Lists Example Code

Explanation: This program creates a list of fruits and demonstrates adding, modifying, removing items, and using list methods.

# Creating a list
fruits = ["apple", "banana", "cherry"]

# Accessing list items
print(fruits[0])  # Output: apple
print(fruits[-1]) # Output: cherry

# Modifying list items
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

# Adding items to a list
fruits.append("orange")
fruits.insert(1, "kiwi")
more_fruits = ["mango", "pineapple"]
fruits.extend(more_fruits)
print(fruits)  # Output: ['apple', 'kiwi', 'blueberry', 'cherry', 'orange', 'mango', 'pineapple']

# Removing items from a list
fruits.remove("kiwi")
print(fruits)  # Output: ['apple', 'blueberry', 'cherry', 'orange', 'mango', 'pineapple']

removed_fruit = fruits.pop(1)
print(removed_fruit)  # Output: blueberry
print(fruits)  # Output: ['apple', 'cherry', 'orange', 'mango', 'pineapple']

del fruits[0]
print(fruits)  # Output: ['cherry', 'orange', 'mango', 'pineapple']

# List methods
print(len(fruits))  # Output: 4

fruits.sort()
print(fruits)  # Output: ['cherry', 'mango', 'orange', 'pineapple']

fruits.reverse()
print(fruits)  # Output: ['pineapple', 'orange', 'mango', 'cherry']




Python Labs

Scroll to Top