Containers are objects that contain other objects
What is an object?
In python, everything is an object. Even the simplest strings and numbers are considered as objects
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'>
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])
#range of indexes
print(pets[1:3])
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']
Deleting Items from a list
pets = ['dog', 'cat', 'rabbit', 'monkey']
pets.pop()
print(pets)
pets = ['dog', 'cat', 'rabbit', 'monkey']
pets.remove('rabbit')
print(pets)
pets = ['dog', 'cat', 'rabbit', 'monkey']
del pets [2]
print(pets)
Getting the length of a list
The length of a list refers to the number of items in a list, use the len()
method
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']
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'>
Sets
- A set is a container/collection that is unordered and immutable
- We create a set using
{}
pets = {'dog', 'cat', 'rabbit'}
print(pets)
# A set can contain objects of different data types
mixed = {'dog', 21, True}
print(mixed)
print(type(mixed))
{True, 'dog', 21}
<class 'set'>
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)
Adding elements to a set
# Adding items to a set
pets = {'dog', 'cat', 'rabbit'}
pets.add('fish')
print(pets)
{'rabbit', 'dog', 'cat', 'fish'}
Removing set elements
# Removing items from a set
pets = {'dog', 'cat', 'rabbit'}
pets.remove('cat') # remove
print(pets)
pets = {'dog', 'cat', 'rabbit'}
pets.discard('rabbit') #discard
print(pets)
pets = {'dog', 'cat', 'rabbit'}
pets.pop() # pop removes the last item from the set
print(pets)
Homework
- Find the length of a set
- Check if an element exists
- Combine sets
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)
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'])
# 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'}
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'])
# Using a dictionary constructer
names = ('a1', 'b2', 'c3')
dictionary = dict(names)
print(dictionary)
{'a': '1', 'b': '2', 'c': '3'}
Back to top