Python Lists

Lists are one of the most versatile and commonly used data structures in Python. A list is an ordered collection of items that can hold a variety of data types, including integers, strings, and even other lists.

Creating a List

You can create a list by placing items inside square brackets [], separated by commas.

# Creating lists
empty_list = []  # An empty list
numbers = [1, 2, 3, 4, 5]  # A list of integers
mixed = [1, "hello", 3.14, True]  # A list with mixed data types

print(numbers)  # Output: [1, 2, 3, 4, 5]

Try It Now

Accessing Elements

You can access elements in a list using their index. Python lists are zero-indexed, meaning the first element has an index of 0.

# Accessing elements
fruits = ["apple", "banana", "cherry"]

print(fruits[0])  # Output: apple
print(fruits[2])  # Output: cherry

Try It Now

Modifying a List

Lists are mutable, which means you can change their contents.

# Modifying elements
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"  # Replace "banana" with "blueberry"

print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

Try It Now

Common List Operations

1. Adding Elements

Use append() to add an item to the end of a list, or insert() to add it at a specific index.

# Adding elements
fruits = ["apple", "banana"]
fruits.append("cherry")  # Adds "cherry" to the end
fruits.insert(1, "blueberry")  # Inserts "blueberry" at index 1

print(fruits)  # Output: ['apple', 'blueberry', 'banana', 'cherry']

Try It Now

2. Removing Elements

Use remove() to delete a specific item, or pop() to remove an item at a specific index.

# Removing elements
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")  # Removes "banana"
last_item = fruits.pop()  # Removes and returns the last item

print(fruits)  # Output: ['apple']
print(last_item)  # Output: cherry

Try It Now

3. Slicing a List

You can slice a list to get a sublist by specifying the start and end indices.

# Slicing a list
numbers = [0, 1, 2, 3, 4, 5, 6]
subset = numbers[2:5]  # Gets elements from index 2 to 4

print(subset)  # Output: [2, 3, 4]

Try It Now

Looping Through a List

You can use a for loop to iterate over the items in a list.

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

for fruit in fruits:
    print(fruit)

Try It Now

List Comprehensions

List comprehensions provide a concise way to create lists.

# List comprehension
squares = [x**2 for x in range(5)]

print(squares)  # Output: [0, 1, 4, 9, 16]

Try It Now

Common List Methods

  • append(item): Adds an item to the end of the list.
  • insert(index, item): Inserts an item at the specified index.
  • remove(item): Removes the first occurrence of the item.
  • pop(index): Removes and returns the item at the specified index.
  • sort(): Sorts the list in ascending order.
  • reverse(): Reverses the elements in the list.

Conclusion

Python lists are incredibly powerful and flexible. Understanding how to create, modify, and manipulate lists will greatly enhance your Python programming skills.