6  Containers

Containers are objects that contain other objects

6.0.1 What is an object?

In python, everything is an object. Even the simplest strings and numbers are considered as objects

6.1 Lists

  • A python list is an ordered container
  • A list is created by using square brackets ([])
  • Objects are poaced inside those brackets and are separated by commas (,)
pets = ['dog', 'cat', 'rabbit', 'monkey']
print(pets)
print(type(pets))
['dog', 'cat', 'rabbit', 'monkey']
<class 'list'>

6.1.0.1 Indexing

  • Indexing is used to access items of a list
  • Indexing uses square brackets and numbers to access individual items of a list
  • Where 0 refers to the first item, 1 refers to the second item, and so on
# indexing
print(pets[2])
rabbit
#range of indexes
print(pets[1:3])
['cat', 'rabbit']

6.1.0.2 Adding items to a list

pets = ['dog', 'cat', 'rabbit', 'monkey']
pets.append('hamster')
print(pets)
['dog', 'cat', 'rabbit', 'monkey', 'hamster']
pets = ['dog', 'cat', 'rabbit', 'monkey']
pets.insert(1, 'hamster')
print(pets)
['dog', 'hamster', 'cat', 'rabbit', 'monkey']

6.1.0.3 Deleting Items from a list

pets = ['dog', 'cat', 'rabbit', 'monkey']
pets.pop()
print(pets)
['dog', 'cat', 'rabbit']
pets = ['dog', 'cat', 'rabbit', 'monkey']
pets.remove('rabbit')
print(pets)
['dog', 'cat', 'monkey']
pets = ['dog', 'cat', 'rabbit', 'monkey']
del pets [2]
print(pets)
['dog', 'cat', 'monkey']

6.1.0.4 Getting the length of a list

The length of a list refers to the number of items in a list, use the len() method

6.1.0.5 Homework

  • Check if an item exist

6.1.0.6 Extending a list

The extend() methods adds all items from one list to another

pets = ['dog', 'cat']
other_pets = ['rabbit', 'monkey']
pets.extend(other_pets)
print(pets)
['dog', 'cat', 'rabbit', 'monkey']

6.2 Tuple

  • Python tuple is an ordered container
  • Its the same as a list but the items of tuples cannot be changed
  • We create a tuple using round brackets ()
pets = ('dog', 'cat', 'rabbit')
print(pets)
print(type(pets))
('dog', 'cat', 'rabbit')
<class 'tuple'>

6.3 Sets

  • A set is a container/collection that is unordered and immutable
  • We create a set using {}
pets = {'dog', 'cat', 'rabbit'}
print(pets)
{'rabbit', 'dog', 'cat'}
# A set can contain objects of different data types
mixed = {'dog', 21, True}
print(mixed)
print(type(mixed))
{True, 'dog', 21}
<class 'set'>

6.3.0.1 Accessing set elements

  • Unlike lists and tuples, you cannot access the items in a set using indexes
  • This is because a set is unordered and not indexed
  • However, we can use a for loop to access all its items one-by-one

Note: We’ll discuss a for loop in the next chapter

# Accessing
pets = {'dog', 'cat', 'rabbit'}
for pet in pets:
    print(pet)
rabbit
dog
cat

6.3.0.2 Adding elements to a set

# Adding items to a set
pets = {'dog', 'cat', 'rabbit'}
pets.add('fish')
print(pets)
{'rabbit', 'dog', 'cat', 'fish'}

6.3.0.3 Removing set elements

# Removing items from a set
pets = {'dog', 'cat', 'rabbit'}
pets.remove('cat') # remove
print(pets)
{'rabbit', 'dog'}
pets = {'dog', 'cat', 'rabbit'}
pets.discard('rabbit') #discard
print(pets)
{'dog', 'cat'}
pets = {'dog', 'cat', 'rabbit'}
pets.pop() # pop removes the last item from the set
print(pets)
{'dog', 'cat'}

6.3.0.4 Homework

  • Find the length of a set
  • Check if an element exists
  • Combine sets

6.3.0.5 Getting the difference between sets

# Getting the difference
first_numbers = {1, 2, 3, 4}
second_numbers = {3, 4, 5, 6}

difference = first_numbers - second_numbers
# another way
difference2 = first_numbers.difference(second_numbers)
print(difference)
{1, 2}

6.4 Dictionaries

A dictionary is an unordered and mutable colletion of items

# Creating 
person = {
    'first_name': 'Voila', 
    'last_name': 'Akullu',
    'age': 16
    }
print(person)
{'first_name': 'Voila', 'last_name': 'Akullu', 'age': 16}
# Accessing items
print(person['last_name'])
Akullu
# Adding items 
person['middle_name'] = 'Vee'
print(person)
{'first_name': 'Voila', 'last_name': 'Akullu', 'age': 16, 'middle_name': 'Vee'}
# Remove items
person.pop('age')
print(person)
{'first_name': 'Voila', 'last_name': 'Akullu', 'middle_name': 'Vee'}

6.4.0.1 Homework

  • Check if an element exists
  • Find the lenght of a dictionary
# Nesting dictionaries
employees = {
    'manager': {
        'name': 'Akullu Viola',
        'age': 29
    },
    'programmer': {
        'name': 'Juma Shafara',
        'age': 30
    }
}

print(employees)
{'manager': {'name': 'Akullu Viola', 'age': 29}, 'programmer': {'name': 'Juma Shafara', 'age': 30}}
# Accessing nested dictionary
programmer = employees['programmer']
print(programmer['name'])
Juma Shafara
# Using a dictionary constructer
names = ('a1', 'b2', 'c3')
dictionary = dict(names)
print(dictionary)
{'a': '1', 'b': '2', 'c': '3'}