CDS-M3
CDS-M3
Dictionaries
Froilan De Guzman
Learning outcomes:
- To differentiate tuples from list
- To enumerate the methods under tuples list.
- To discuss the set methods and operations
- To discuss the use of dictionaries.
2
Python Tuples
“A Tuple is a collection of Python objects
separated by commas. In some ways a
tuple is similar to a list in terms of
indexing, nested objects and repetition
but a tuple is immutable unlike lists
which are mutable. Note: In case your
generating a tuple with a single element,
make sure to add a comma after the
element.”
Source: geeksforgeeks.org
3
Basic tuple declaration
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with
parenthesis.
#Creating and printing a tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple)
#Printing some data items on the tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
#Negative indexing
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
#Tuple slicing
thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
4
Source: w3schools.com
Tuple is not changeable but…
Once a tuple is created, you cannot change its values. Tuples
are unchangeable, or immutable as it also is called. But there is a
workaround. You can convert the tuple into a list, change the list,
and convert the list back into a tuple.
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Source: w3schools.com
5
Some tuple operations
#Joining two tuples
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
#Count method
print(tuple3.count("b"))
#Index method
print(tuple3.index(“c"))
Source: w3schools.com
6
Python sets
A set is a collection which is unordered and unindexed. In Python sets are written
with curly brackets.
thisset = {"apple", "banana", "cherry"}
print(thisset)
Once a set is created, you cannot change its items, but you can add new items.
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
But you can remove item
thisset = {"apple", "banana", "cherry"}
thisset.remove(“banana")
print(thisset)
You can also use discard
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
If the item to remove does not exist, remove() will raise an error.
If the item to remove does not exist, discard() will NOT raise an error.
Source: w3schools.com 7
More on Python set methods
You can also use the pop(), method to remove an item, but this method will remove the
last item. Remember that sets are unordered, so you will not know what item that gets
removed. The return value of the pop() method is the removed item.
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
The clear() method empties the set:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
The del keyword will delete the set completely:
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
#this will raise an error because the set no longer exists
Source: w3schools.com
8
Joining of sets
The union() method returns a new set with all items from both sets:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
The update() method inserts the items in set2 into set1:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
Note: Both union() and update() will exclude any duplicate items.
Source: w3schools.com
9
Set Operations
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print(A | B)
# Union of sets: {1, 2, 3, 4, 5, 6, 7, 8}
print(A & B)
# Intersection of sets: {4, 5}
print(A - B)
# Difference of sets: {1, 2, 3}
print(A ^ B)
# Symmetric difference: {1, 2, 3, 6, 7, 8}
10
Conversions: List, Tuple, Sets
listA = [1, 2, 3, 4, 5]
tupA = tuple(listA)
setA = set(listA)
print(listA)
print(tupA)
print(setA)
tupB = (6, 7, 8, 9, 10)
listB = list(tupB)
setB = set(tupB)
print(listB)
print(tupB)
print(setB)
setC = {11, 12, 13, 14, 15}
listC = list(setC)
tupC = tuple(setC)
print(listC)
print(tupC)
11
print(setC)
Python dictionary
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries
are written with curly brackets, and they have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
#Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
12
Basic dictionary operations
#declaration: #other ways to display dictionary
dict1 = dict(froi=5, larry=4, erwin=6); for key in dict1:
print (dict1); print (key, dict1[key]);
#updating values from keys:
dict1['larry']=8; for k, v in dict1.items():
print (dict1); print (k, v);
#counting records on dictionary
print (len(dict1)); #adding new items on the dictionary
#displaying keys, values, items dict1['ryan']=8;
print (dict1.keys()); dict1['bryan']=7;
print (dict1.values()); print (dict1);
print (dict1.items());
13
References:
- programiz.com
- w3schools.com
- geeksforgeeks.org
14