Python Lists – Working with Collections of Data

Lists are one of Python’s most useful data structures. They allow you to store multiple items in a single variable and keep them organized in a specific order.

Creating Lists

Create a list by placing items inside square brackets, separated by commas:

fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, 3.14]

Accessing List Items

Use index numbers to access specific items. Python starts counting from 0:

print(fruits[0])  # "apple"
print(fruits[1])  # "banana"
print(fruits[-1]) # "orange" (last item)

Modifying Lists

Lists are mutable, meaning you can change them after creation:

# Add items
fruits.append("grape")
fruits.insert(1, "kiwi")

# Remove items
fruits.remove("banana")
last_fruit = fruits.pop()

# Change items
fruits[0] = "pineapple"

Useful List Methods

numbers = [3, 1, 4, 1, 5]
print(len(numbers))      # 5 (length)
print(numbers.count(1))  # 2 (occurrences of 1)
numbers.sort()           # [1, 1, 3, 4, 5]
numbers.reverse()        # [5, 4, 3, 1, 1]

Lists are perfect for storing related data like shopping lists, student names, or game scores.

Author

  • Mohammad Golam Dostogir, Software Engineer specializing in Python, Django, and AI solutions. Active contributor to open-source projects and tech communities, with experience delivering applications for global companies.
    GitHub

    View all posts
Index