Python Lists

To store a collection of items in Python you can use Lists. Square brackets [] indicate a list, you can put list items between square brackets separated by commas. You can store different types of items in a list.

colors = ["red", "blue", "yellow", "green", "orange"]
primes = [2, 3, 5, 7, 11]
mixed = ["red", 2, "blue", 3, "yellow", 5]

Indexing in Lists

Python uses zero-based indexing, so the first item in a list has index value 0.

colors=["red", "blue", "yellow"]     # index of "red" is 0
                                     # index of "blue" is 1
                                     # index of "yellow" is 2

Negative indexes are also used in Python. Negative indexes start from the end of list (-1 refers to the last item, -2 refers to second last item).

colors=["red", "blue", "yellow"]    # Both 2 and -1 index values
                                    # refer to same item ("yellow")
                                    # colors[-1] is "yellow"

Accessing Items in a List

You can access items in a list using index numbers.

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

print("First item in the list is : " + colors[0])

# First item in the list is : red

Updating Items in a List

You can change an item in a list using its index value.

colors = ["red", "blue", "yellow"]
colors[0] = "purple"

# updated list is : ["purple", "blue", "yellow"]

Slicing

In a slice, you can get a part of a list. To slice a list in Python, you can square brackets []. Inside square brackets, you specify two integers separated by a colon. First integer shows where slicing begins, second integer shows where slicing ends. These integers refer to index values of list items.

colors = ["red", "blue", "yellow", "green", "orange"]
print(colors[0:2])
# ["red", "blue"]
# index 0 is first item (inclusive)
# index 2 is third item (exclusive)

print(colors[:2])
# ["red", "blue"]
# Same as above. If first index is not specified,
# slicing starts from index 0.

print(colors[2:3])
# ["yellow"]

print(colors[2:])
# ["yellow", "green", "orange"]
# If second index is not specified,
# slicing ends at the last item ("orange")

Iterating a List

To iterate items in a list, you can use a for loop.

colors = ["red", "blue", "yellow"]
for color in colors:
  print("color is : " + color)

# prints:
# color is : red
# color is : blue
# color is : yellow

Adding Items

Lists are dynamic objects. You can add new items to an existing list. To add a new item, append() method is used.

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

# new list is : ["red", "blue", "yellow", "green"]

To add a new item at a specified position, you can use insert() method in Python.

colors = ["red", "blue", "yellow"]
colors.insert(0, "green")

# New item is inserted at index position 0.
# New list is : ["green", "red", "blue", "yellow"]

Removing Items

Similar to adding new items, you can remove existing items in a list using remove() method.

colors = ["red", "blue", "yellow", "green"]
colors.remove("green")
print(colors)

# New list is : ["red", "blue", "yellow"]