0% found this document useful (0 votes)
4 views36 pages

dictionary1+python-merged

The document provides an overview of Python dictionaries, including their structure, methods, and examples of usage. It explains how dictionaries store data as key-value pairs and demonstrates various operations such as adding, deleting, and accessing elements. Additionally, it briefly introduces tuples as immutable lists and highlights their characteristics.

Uploaded by

jackyman371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views36 pages

dictionary1+python-merged

The document provides an overview of Python dictionaries, including their structure, methods, and examples of usage. It explains how dictionaries store data as key-value pairs and demonstrates various operations such as adding, deleting, and accessing elements. Additionally, it briefly introduces tuples as immutable lists and highlights their characteristics.

Uploaded by

jackyman371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

A+ Computer Science

Dictionaries
Dictionaries
Key Value
restroom bano
cat gato
boy muchacho
house casa
toad sapo
water agua

© A+ Computer Science -
Dictionary
Python dictionaries were built using
hash tables.

A hash table is a giant array. Each


item is inserted into the array
according to a hash formula.

0 1 2 3 4

© A+ Computer Science -
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'three': 3, 'one': 1, 'two': 2, 'four': 4}

"two": 2,
"three": 3,
"four" : 4
Dictionaries store material as
} key:value
print( map )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 1

"two": 2,
"three": 3,
"four" : 4
}
print( map["one"] )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1, 'two': 333}

"two": 2,
}
map["two"] = 333;
print( map )

© A+ Computer Science -
Dictionaries
map = {} OUTPUT
map[45]="at"; at
go
map[17]="go"
print( map[45] )
print( map[17] )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1}

"two": 2,
}

del map["two"]
print( map )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1}

"two": 2,
}

if "two" in map:
del map["two"]
print( map )

© A+ Computer Science -
© A+ Computer Science -
Dictionaries
frequently used methods

Name Use

get(x) gets the value for key x


items() returns all of the key value pairs
values() returns a list of all of the values
len(x) returns the # of pairs in the dictionary
keys() returns a list of all of the keys
pop(x) removes the pair with key x

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 1

"two": 2,
}

print( map.get("one" ) )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1}

"two": 2,
}

map.pop("two")
print( map )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 2

"two": 2,
}

print( len(map) )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, ('one', 1)
('two', 2)
"two": 2,
}

for pair in map.items() :


print( pair )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 2
1
"two": 2,
}

for val in map.values() :


print( val )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, one
two
"two": 2,
}

for key in map.keys() :


print( key )

© A+ Computer Science -
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, one
two
"two": 2,
}
for key in map :
print( key )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, one
two
"two": 2,
}
for x in map.keys() :
print( x )
Note: The previous slide did the exact same thing. When you loop
through a dictionary without calling a method, you loop through
the keys.

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 1
2
"two": 2,
} Using the keys as indexes, we can
for key in map : receive the values
print( map[key] )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 2
1
"two": 2,
}

for val in map.values() : Values can also be accessed


directly using the values method
print( val )

© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, ('one', 1)
('two', 2)
"two": 2,
}

for pair in map.items() : Key:Value combinations are


print( pair ) accessed using the items
method

© A+ Computer Science -
Building a Dictionary
map = {}
OUTPUT
{2: 5, 3: 2, 4: 1, 5: 1, 6: 1,
list = [3,4,5,6,7,8,2,2,2,2,2,3]
7: 1, 8: 1}
for x in list :
if not x in map : x is not in the dictionary
map[x] = 0 set the key to zero
map[x] = map[x] + 1 add one to the key

print( map )

© A+ Computer Science -
Example 1
Example 2
Example 3
Tuples
Tuples are lists that cannot be changed

myTuple = (255, 0, 89)

print(myTuple)

Output
(255, 0, 89)

© A+ Computer Science -
Tuples
The data is accessed with indices

myTuple = (255, 0, 89)

print(myTuple[2])

Output
89

© A+ Computer Science -
Tuples
Since tuples cannot be changed, they have no methods

myTuple = (255, 0, 89)

myTuple.append(35)

AttributeError: 'tuple' object has no attribute 'append'

© A+ Computer Science -
Immutable (not changeable)
Indexing/slicing
summary

You might also like