CodingNic

Data Structures

Lists

Data Structures 28 min read

Lists

Lists

Sometimes one variable is not enough.

You may want to store:

  • Many names
  • Many numbers
  • Many products
  • Many tasks

Creating many separate variables becomes hard to manage.

Python gives us lists to solve this problem.

What Is a List?

A list is a collection of values stored in one variable.

Lists use square brackets [].

python
names = ["Tom", "Sara", "Mike"]
print(names)

Output

text
['Tom', 'Sara', 'Mike']

Why Lists Matter

Lists help you:

  • Store many values together
  • Keep data organized
  • Add new items
  • Remove items
  • Change items
  • Search data
  • Loop through items easily

Creating Lists

python
numbers = [10, 20, 30]
colors = ["Red", "Blue", "Green"]
mixed = ["Book", 10, True]

A list can store text, numbers, booleans, or mixed values.

Reading Items with Indexes

Each item has a position called an index.

The first item starts at 0.

python
fruits = ["Apple", "Banana", "Orange"]

print(fruits[0])
print(fruits[1])
print(fruits[2])

Output

text
Apple
Banana
Orange

Negative Indexes

Negative indexes count from the end.

  • -1 last item
  • -2 second last item
python
print(fruits[-1])
print(fruits[-2])

Output

text
Orange
Banana

Updating Items

Lists can be changed.

python
fruits = ["Apple", "Banana", "Orange"]
fruits[1] = "Grape"

print(fruits)

Output

text
['Apple', 'Grape', 'Orange']

Adding Items with append()

Adds one item to the end.

python
fruits = ["Apple", "Banana"]
fruits.append("Orange")

print(fruits)

Output

text
['Apple', 'Banana', 'Orange']

Adding Items with insert()

Adds an item at a chosen position.

python
fruits = ["Apple", "Orange"]
fruits.insert(1, "Banana")

print(fruits)

Output

text
['Apple', 'Banana', 'Orange']

Adding Many Items with extend()

Adds multiple items.

python
fruits = ["Apple"]
fruits.extend(["Banana", "Orange"])

print(fruits)

Output

text
['Apple', 'Banana', 'Orange']

Removing with remove()

Removes by value.

python
fruits = ["Apple", "Banana", "Orange"]
fruits.remove("Banana")

print(fruits)

Output

text
['Apple', 'Orange']

Removing with pop()

Removes by index.

python
fruits = ["Apple", "Banana", "Orange"]
fruits.pop(1)

print(fruits)

Output

text
['Apple', 'Orange']

Clearing a List

Removes all items.

python
items = ["Pen", "Book"]
items.clear()

print(items)

Output

text
[]

Finding Item Position

Use index().

python
fruits = ["Apple", "Banana", "Orange"]
print(fruits.index("Banana"))

Output

text
1

Counting Items

Use count().

python
numbers = [1, 2, 2, 3]
print(numbers.count(2))

Output

text
2

Sorting Items

Use sort().

python
numbers = [3, 1, 2]
numbers.sort()

print(numbers)

Output

text
[1, 2, 3]

Reversing Items

Use reverse().

python
numbers = [1, 2, 3]
numbers.reverse()

print(numbers)

Output

text
[3, 2, 1]

What Is Slicing?

Slicing means taking part of a list.

Basic format:

python
list[start:stop:step]
  • start = where to begin
  • stop = where to stop (not included)
  • step = how many positions to move

Use this list:

python
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango"]

Slicing with Start and Stop

python
print(fruits[1:4])

Output

text
['Banana', 'Orange', 'Grape']

From Beginning

python
print(fruits[:3])

Output

text
['Apple', 'Banana', 'Orange']

To the End

python
print(fruits[2:])

Output

text
['Orange', 'Grape', 'Mango']

Copy a List

python
print(fruits[:])

Output

text
['Apple', 'Banana', 'Orange', 'Grape', 'Mango']

Using Step

Take every second item.

python
print(fruits[::2])

Output

text
['Apple', 'Orange', 'Mango']

Start, Stop, Step Together

python
print(fruits[1:5:2])

Output

text
['Banana', 'Grape']

Reverse with Slicing

python
print(fruits[::-1])

Output

text
['Mango', 'Grape', 'Orange', 'Banana', 'Apple']

Looping Through a List

python
names = ["Tom", "Sara", "Mike"]

for name in names:
    print(name)

Output

text
Tom
Sara
Mike

List Length

Use len().

python
items = ["Pen", "Book", "Bag"]
print(len(items))

Output

text
3

Code Along

python
cities = ["New York", "Toronto", "Chicago"]
cities.append("Vancouver")

for city in cities:
    print(city)

Output

text
New York
Toronto
Chicago
Vancouver

Mini Challenge

Build a shopping list.

Steps:

  • Create a list with:
    Milk, Bread, Eggs
  • Add Juice
  • Remove Bread
  • Print the first two items using slicing
  • Print the final list

Expected output:

text
['Milk', 'Eggs']
['Milk', 'Eggs', 'Juice']

Real World Use Case

Programs use lists for shopping carts, contact lists, tasks, products, and storing many records.

Quiz

  1. What is a list?
  2. What index number is the first item?
  3. What does append() do?
  4. What does slicing do?

Assignment

Create a list of 5 favorite movies. Add one movie, remove one movie, then print the first three items using slicing.

Summary

You learned how to create lists, read items, update data, remove data, use methods, slice lists, and loop through list values.