Python Lists

Introduction to Lists

In real life, we often need to keep track of multiple items together. For example, a list of groceries, names of your friends, or favorite movies.

In Python, we use something called a list to store many items in one place.

What is a List?

A list is a collection of items. These items are stored in a specific order, and you can add, change, or remove items whenever you want.

Think of it like a shopping list you can edit!

Nature of Lists

  • Lists can hold different types of items (strings, numbers, even other lists!).
  • Lists are ordered. This means the position of each item is important.
  • Lists can be changed (we can add, remove, or update items).

Basic List Operations (CRUD)

1. Create List

Lists in Python are defined using square brackets [], and the elements are separated by commas. The order of items matters and can be accessed by index.

  colors = ["red", "green", "blue"]

2. Read/Access Items in List

We can access items in a list using their position, which is called an index. Indexing in Python starts from 0, so the first element is at index 0, the second at 1, and so on.

  colors = ["red", "green", "blue"]
print(colors[0])  # First item
print(colors[1])  # Second item
print(colors[2])  # Third item
red
green
blue

Here, colors[0] gives us "red", colors[1] gives "green", and colors[2] gives "blue". Trying to access an index that doesn’t exist (like colors[3]) will raise an IndexError.

3. Update/Change Items in List

We can change an item in the list by assigning a new value to a specific index. In this example, colors[1] refers to the second item in the list (indexing starts at 0). The value at that index, which was originally "green", is replaced with "yellow". This operation directly modifies the list in-place without creating a new list.

  colors = ["red", "green", "blue"]
colors[1] = "yellow"
print(colors)
['red', 'yellow', 'blue']

We changed the second item ("green") to "yellow".

4. Delete/Remove Items from List

The del keyword is used to delete an item at a specific index in a list. In the example, del colors[2] removes the item at index 2, which is "blue". After this operation, the list only contains ["red", "green"]. The list size decreases, and all elements after the deleted index shift one position to the left. The syntax is: del list[index]. This operation is permanent and does not return the deleted item.

  colors = ["red", "green", "blue"]
del colors[2]
print(colors)
['red', 'yellow']

We deleted the third item ("blue"). Now only two colors are left.

Adding Items

Use append() to add a new item at the end:

  colors = ["red", "green", "blue"]
colors.append("purple")
print(colors)
['red', 'yellow', 'purple']

Checking the Length of a List

Use len() to find how many items are in the list:

  colors = ["red", "green", "blue"]
print(len(colors))
3

Summary

  • A list holds multiple items in one place.
  • Items are ordered and can be changed.
  • You can create, read, update, and delete items in a list.