Skip to content

Python Lists (List)

Lists are one of the most fundamental and commonly used data structures in Python. They are ordered and mutable collections that can contain objects of any type.

Creating Lists

Lists are defined using square brackets [], with elements separated by commas ,.

python
# Create an empty list
empty_list = []

# List containing integers
numbers = [1, 2, 3, 4, 5]

# List containing strings
fruits = ["apple", "banana", "cherry"]

# List containing mixed data types
mixed_list = [1, "hello", 3.14, True]

# Can also use list() constructor
from_tuple = list((1, 2, 3)) # Create list from tuple

Accessing List Elements

Just like strings, you can access list elements through indexing and slicing.

Indexing

python
fruits = ["apple", "banana", "cherry"]

print(fruits[0])   # Output: 'apple'
print(fruits[1])   # Output: 'banana'
print(fruits[-1])  # Output: 'cherry' (last element)

Slicing

python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Get elements from index 2 to 4
print(numbers[2:5])  # Output: [2, 3, 4]

# Get the first 3 elements
print(numbers[:3])   # Output: [0, 1, 2]

# Get elements from index 5 to the end
print(numbers[5:])   # Output: [5, 6, 7, 8, 9]

# Slice with step 2
print(numbers[::2])  # Output: [0, 2, 4, 6, 8]

Lists are Mutable

Unlike strings and tuples, lists are mutable. This means you can modify, add, and remove elements from the list.

python
fruits = ["apple", "banana", "cherry"]

# Modify element
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

Common List Methods

Python provides many built-in methods for lists.

  • list.append(x): Adds an element x at the end of the list.
  • list.insert(i, x): Inserts an element x at the specified position i.
  • list.extend(iterable): Adds all elements from an iterable (such as another list) to the end of the list.
  • list.remove(x): Removes the first element with value x from the list.
  • list.pop(i): Removes and returns the element at the specified position i. If no index is specified, removes and returns the last element.
  • list.clear(): Removes all elements from the list.
  • list.index(x): Returns the index of the first element with value x in the list.
  • list.count(x): Returns the number of times element x appears in the list.
  • list.sort(): Sorts the list in place (modifies the original list).
  • list.reverse(): Reverses the elements in the list in place.
  • list.copy(): Returns a shallow copy of the list.

Example:

python
my_list = [3, 1, 4]

# Add element
my_list.append(1)
print(my_list) # Output: [3, 1, 4, 1]

# Insert element
my_list.insert(2, 5) # Insert 5 at index 2
print(my_list) # Output: [3, 1, 5, 4, 1]

# Remove element
my_list.remove(1) # Remove the first 1
print(my_list) # Output: [3, 5, 4, 1]

# Pop element
popped_item = my_list.pop()
print(f"Popped: {popped_item}, List is now: {my_list}") # Popped: 1, List is now: [3, 5, 4]

# Sort
my_list.sort()
print(my_list) # Output: [3, 4, 5]

List Operations

  • Concatenation: Using the + operator to concatenate two lists creates a new list.
  • Repetition: Using the * operator to repeat list elements.
python
list1 = [1, 2]
list2 = [3, 4]

print(list1 + list2) # Output: [1, 2, 3, 4]
print(list1 * 3)     # Output: [1, 2, 1, 2, 1, 2]

Content is for learning and research only.