0% found this document useful (0 votes)
8 views91 pages

Module 2 Python Datastructure

The document provides an overview of Python data structures, focusing on strings and lists. It explains string operations, including concatenation, indexing, slicing, and various string methods, as well as the mutability of lists and how to manipulate them using indices. Additionally, it covers basic algorithms and the concept of collections in programming.

Uploaded by

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

Module 2 Python Datastructure

The document provides an overview of Python data structures, focusing on strings and lists. It explains string operations, including concatenation, indexing, slicing, and various string methods, as well as the mutability of lists and how to manipulate them using indices. Additionally, it covers basic algorithms and the concept of collections in programming.

Uploaded by

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

Python Data Structures

Strings, List, Dictionary, Tuple


Strings
String Data Type
>>> str1 = "Hello"
>>> str2 = 'there'
>>> bob = str1 + str2
>>> print(bob)
• A string is a sequence of characters Hellothere
>>> str3 = '123'
• A string literal uses quotes >>> str3 = str3 + 1
'Hello' or "Hello" Traceback (most recent call
last): File "<stdin>", line 1,
• For strings, + means “concatenate” in <module>
TypeError: cannot concatenate
• When a string contains numbers, it is 'str' and 'int' objects
still a string >>> x = int(str3) + 1
>>> print(x)
• We can convert numbers in a string 124
>>>
into a number using int()
Reading and >>> name = input('Enter:')
Enter:Chuck
Converting >>> print(name)
Chuck
>>> apple = input('Enter:')
• We prefer to read data in using
Enter:100
strings and then parse and
>>> x = apple – 10
convert the data as we need
Traceback (most recent call
• This gives us more control over last): File "<stdin>", line 1,
in <module>
error situations and/or bad user
TypeError: unsupported operand
input
type(s) for -: 'str' and 'int'
• Input numbers must be >>> x = int(apple) – 10
>>> print(x)
converted from strings
90
Looking Inside Strings
• We can get at any single character in a b a n a n a
string using an index specified in 0 1 2 3 4 5
square brackets
>>> fruit = 'banana'
• The index value must be an integer >>>
>>>
letter = fruit[1]
print(letter)
and starts at zero a
>>> x = 3
• The index value can be an expression >>> w = fruit[x - 1]
that is computed >>> print(w)
n
A Character Too Far
• You will get a python error >>> zot = 'abc'
>>> print(zot[5])
if you attempt to index
Traceback (most recent call
beyond the end of a string last): File "<stdin>", line
1, in <module>
• So be careful when IndexError: string index out
constructing index values of range
and slices >>>
Strings Have Length

b a n a n a
The built-in function len gives 0 1 2 3 4 5
us the length of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.

'banana' len() 6
(a number)
(a string) function
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
def len(inp):
blah
'banana' blah 6
(a string) for x in y:
blah
(a number)
blah
Looping Through Strings

Using a while statement, fruit = 'banana'


0b
an iteration variable, and index = 0 1a
the len function, we can while index < len(fruit): 2n
construct a loop to look at letter = fruit[index] 3a
print(index, letter) 4n
each of the letters in a index = index + 1
string individually 5a
Looping Through Strings

• A definite loop using a b


for statement is much a
more elegant fruit = 'banana'
for letter in fruit: n
a
• The iteration variable is print(letter)
n
completely taken care of a
by the for loop
Looping Through Strings

• A definite loop using a fruit = 'banana'


for letter in fruit :
b
for statement is much a
print(letter)
more elegant n
a
• The iteration variable is index = 0 n
completely taken care of while index < len(fruit) :
a
by the for loop letter = fruit[index]
print(letter)
index = index + 1
Looping and Counting
word = 'banana'
This is a simple loop that count = 0
loops through each letter in a for letter in word :
string and counts the number if letter == 'a' :
of times the loop encounters count = count + 1
the 'a' character print(count)
Looking Deeper into in
• The iteration variable “iterates”
through the sequence Iteration Six-character
(ordered set) variable string
• The block (body) of code is
executed once for each value for letter in 'banana' :
in the sequence
print(letter)
• The iteration variable moves
through all of the values in the
sequence
Yes No b a n a n a
Done? Advance letter

print(letter)

for letter in 'banana' :


print(letter)

The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequence
More String Operations
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
continuous section of a string
using a colon operator >>> s = 'Monty Python'
>>> print(s[0:4])
• The second number is one Mont
beyond the end of the slice - >>> print(s[6:7])
“up to but not including” P
• If the second number is >>> print(s[6:20])
beyond the end of the string, Python
it stops at the end
Slicing Strings M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11

>>> s = 'Monty Python'


If we leave off the first number >>> print(s[:2])
or the last number of the slice, Mo
it is assumed to be the >>> print(s[8:])
beginning or end of the string thon
respectively
>>> print(s[:])
Monty Python
String Concatenation
>>> a = 'Hello'
>>> b = a + 'There'
When the + operator is >>> print(b)
applied to strings, it means HelloThere
“concatenation” >>> c = a + ' ' + 'There'
>>> print(c)
Hello There
>>>
Using in as a Logical Operator
>>> fruit = 'banana'
• The in keyword can also be >>> 'n' in fruit
used to check to see if one True
string is “in” another string >>> 'm' in fruit
False
• The in expression is a >>> 'nan' in fruit
True
logical expression that >>> if 'a' in fruit :
returns True or False and ... print('Found it!')
can be used in an if ...
statement Found it!
>>>
String Comparison
if word == 'banana':
print('All right, bananas.')

if word < 'banana':


print('Your word,' + word + ', comes before banana.')
elif word > 'banana':
print('Your word,' + word + ', comes after banana.')
else:
print('All right, bananas.')
• Python has a number of string String Library
functions which are in the
string library
>>> greet = 'Hello Bob'
• These functions are already >>> zap = greet.lower()
built into every string - we >>> print(zap)
invoke them by appending the hello bob
function to the string variable >>> print(greet)
Hello Bob
• These functions do not modify >>> print('Hi There'.lower())
the original string, instead they hi there
return a new string that has >>>
been altered
>>> stuff = 'Hello world'
>>> type(stuff)
<class 'str'>
>>> dir(stuff)
[...'capitalize', 'casefold', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map',
'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']

https://docs.python.org/3/library/stdtypes.html#string-methods
String Library
str.capitalize() str.replace(old, new[, count])
str.center(width[, fillchar]) str.lower()
str.endswith(suffix[, start[, end]]) str.rstrip([chars])
str.find(sub[, start[, end]]) str.strip([chars])
str.lstrip([chars]) str.upper()
Searching a String
b a n a n a
• We use the find() function to search
for a substring within another string
0 1 2 3 4 5

• find() finds the first occurrence of the >>> fruit = 'banana'


substring >>> pos = fruit.find('na')
>>> print(pos)
• If the substring is not found, find() 2
returns -1 >>> aa = fruit.find('z')
>>> print(aa)
• Remember that string position starts -1
at zero
Making everything UPPER CASE

• You can make a copy of a >>> greet = 'Hello Bob'


string in lower case or upper >>> nnn = greet.upper()
case >>> print(nnn)
• Often when we are searching HELLO BOB
>>> www = greet.lower()
for a string using find() we first
>>> print(www)
convert the string to lower case
hello bob
so we can search a string
>>>
regardless of case
Search and Replace
• The replace() function
is like a “search and >>> greet = 'Hello Bob'
replace” operation in a >>> nstr = greet.replace('Bob','Jane')
>>> print(nstr)
word processor
Hello Jane

• It replaces all >>> nstr = greet.replace('o','X')


>>> print(nstr)
occurrences of the HellX BXb
search string with the >>>
replacement string
Stripping Whitespace
• Sometimes we want to take
a string and remove
whitespace at the beginning >>> greet = ' Hello Bob '
and/or end >>> greet.lstrip()
'Hello Bob '

• lstrip() and rstrip() remove >>> greet.rstrip()


' Hello Bob'
whitespace at the left or right >>> greet.strip()
'Hello Bob'
• strip() removes both >>>
beginning and ending
whitespace
Prefixes
>>> line = 'Please have a nice day'
>>> line.startswith('Please')
True
>>> line.startswith('p')
False
Parsing and
21 31 Extracting
From [email protected] Sat Jan 5 09:14:16 2008

>>> data = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> atpos = data.find('@')
>>> print(atpos)
21
>>> sppos = data.find(' ',atpos)
>>> print(sppos)
31
>>> host = data[atpos+1 : sppos]
>>> print(host)
uct.ac.za
Summary
• String type • String operations
• Read/Convert • String library
• Indexing strings [] • String comparisons
• Slicing strings [2:4] • Searching in strings
• Looping through strings • Replacing text
with for and while • Stripping white space
• Concatenating strings with +
Python Lists
Programming
Algorithm
- A set of rules or steps used to solve a problem

Data Structure
- A particular way of organizing data in a computer
What is Not a “Collection”?
Most of our variables have one value in them - when we put a new
value in the variable, the old value is overwritten

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of Collection
• A collection allows us to put many values in a single “variable”

• A collection is nice because we can carry all many values


around in one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


List Constants
List constants are surrounded by >>> print([1, 24, 76])
square brackets and the elements [1, 24, 76]
>>> print(['red', 'yellow',
in the list are separated by
'blue'])
commas ['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
A list element can be any Python ['red', 24, 98.6]
>>> print([ 1, [5, 6], 7])
object - even another list
[1, [5, 6], 7]
>>> print([])
A list can be empty []
We Already Use Lists!
5
for i in [5, 4, 3, 2, 1] :
print(i)
4
print('Blastoff!') 3
2
1
Blastoff!
Lists and Definite Loops

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends : Happy New Year: Joseph
print('Happy New Year:', friend)
print('Done!') Happy New Year: Glenn
Happy New Year: Sally
Done!
z = ['Joseph', 'Glenn', 'Sally']
for x in z:
print('Happy New Year:', x)
print('Done!')
Looking Inside Lists
Just like strings, we can get at any single element in a list using an
index specified in square brackets

>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]


Joseph Glenn Sally >>> print(friends[1])
Glenn
0 1 2 >>>
Lists are Mutable
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
Strings are “immutable” - we cannot Traceback
TypeError: 'str' object does not
change the contents of a string - support item assignment
we must make a new string to >>> x = fruit.lower()
make any change >>> print(x)
banana
Lists are “mutable” - we can change >>> lotto = [2, 14, 26, 41, 63]
>>> print(lotto)
an element of a list using the index [2, 14, 26, 41, 63]
operator >>> lotto[2] = 28
>>> print(lotto)
[2, 14, 28, 41, 63]
How Long is a List?

The len() function takes a list as a >>> greet = 'Hello Bob'


parameter and returns the number >>> print(len(greet))
of elements in the list 9
>>> x = [ 1, 2, 'joe', 99]
>>> print(len(x))
Actually len() tells us the number of 4
elements of any set or sequence >>>
(such as a string...)
Using the range Function
The range function returns a
>>> print(range(4))
list of numbers that range [0, 1, 2, 3]
from zero to one less than >>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print(len(friends))
the parameter 3
>>> print(list(range(len(friends))))
[0, 1, 2]
We can construct an index >>>
loop using for and an
integer iterator
A Tale of Two Loops...
>>> friends = ['Joseph', 'Glenn', 'Sally']
friends = ['Joseph', 'Glenn', 'Sally'] >>> print(len(friends))
3
for friend in friends : >>> print(list(range(len(friends))))
print('Happy New Year:', friend) [0, 1, 2]
>>>
for i in range(len(friends)) :
friend = friends[i]
print('Happy New Year:', friend) Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Concatenating Lists Using +
>>> a = [1, 2, 3]
We can create a new list >>> b = [4, 5, 6]
by adding two existing >>> c = a + b
lists together >>> print(c)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
Lists Can Be Sliced Using :
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41,12] Remember: Just like in
>>> t[:4] strings, the second
[9, 41, 12, 3] number is “up to but not
>>> t[3:] including”
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods
>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
[... 'append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']
>>>

http://docs.python.org/tutorial/datastructures.html
Building a List from Scratch
>>> stuff = list()
We can create an empty list and >>> stuff.append('book')
then add elements using the >>> stuff.append(99)
append method >>> print(stuff)
['book', 99]
The list stays in order and new >>> stuff.append('cookie')
elements are added at the >>> print(stuff)
['book', 99, 'cookie']
end of the list
Is Something in a List?
Python provides two operators >>> some = [1, 9, 21, 10, 16]
that let you check if an item is >>> 9 in some
in a list True
>>> 15 in some
False
These are logical operators that >>> 20 not in some
return True or False True
>>>
They do not modify the list
Lists are in Order
• A list can hold many
items and keeps those
items in the order until >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
we do something to >>> friends.sort()
change the order >>> print(friends)
['Glenn', 'Joseph', 'Sally']
• A list can be sorted >>> print(friends[1])
Joseph
(i.e., change its order) >>>
• The sort method
(unlike in strings)
means “sort yourself”
Built-in Functions and Lists
>>> nums = [3, 41, 12, 9, 74, 15]
There are a number of >>> print(len(nums))
functions built into Python 6
that take lists as >>> print(max(nums))
parameters 74
>>> print(min(nums))
3
Remember the loops we
>>> print(sum(nums))
built? These are much 154
simpler. >>> print(sum(nums)/len(nums))
25.6
total = 0 Enter a number: 3
count = 0
while True : Enter a number: 9
inp = input('Enter a number: ') Enter a number: 5
if inp == 'done' : break
value = float(inp) Enter a number: done
total = total + value Average: 5.66666666667
count = count + 1

average = total / count


numlist = list()
print('Average:', average)
while True :
inp = input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)

average = sum(numlist) / len(numlist)


print('Average:', average)
Best Friends: Strings and Lists
>>> abc = 'With three words' >>> print(stuff)
>>> stuff = abc.split() ['With', 'three', 'words']
>>> print(stuff) >>> for w in stuff :
['With', 'three', 'words'] ... print(w)
>>> print(len(stuff)) ...
3 With
>>> print(stuff[0]) Three
With Words
>>>

Split breaks a string into parts and produces a list of strings. We think of these
as words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces'] ● When you do not specify a
>>>
>>> line = 'first;second;third' delimiter, multiple spaces are
>>> thing = line.split()
>>> print(thing) treated like one delimiter
['first;second;third']
>>> print(len(thing))
1 ● You can specify what delimiter
>>> thing = line.split(';')
>>> print(thing) character to use in the splitting
['first', 'second', 'third']
>>> print(len(thing))
3
>>>
From [email protected] Sat Jan 5 09:14:16 2008

fhand = open('mbox-short.txt') Sat


for line in fhand:
line = line.rstrip() Fri
if not line.startswith('From ') : continue Fri
words = line.split()
print(words[2]) Fri
...

>>> line = 'From [email protected] Sat Jan 5 09:14:16 2008'


>>> words = line.split()
>>> print(words)
['From', '[email protected]', 'Sat', 'Jan', '5', '09:14:16', '2008']
>>>
List Summary
• Concept of a collection • Slicing lists

• Lists and definite loops • List methods: append, remove

• Indexing and lookup • Sorting lists

• List mutability • Splitting strings into lists of words

• Functions: len, min, max, sum • Using split to parse strings


Python Dictionaries
What is a Collection?
• A collection is nice because we can put more than one value in it
and carry them all around in one convenient package

• We have a bunch of values in a single “variable”

• We do this by having more than one place “in” the variable

• We have ways of finding the different places in the variable


A Story of Two Collections..
• List

A linear collection of values


Lookup by position 0 .. length-1

• Dictionary

A linear collection of key-value pairs


Lookup by "tag" or "key"
https://en.wikipedia.org/wiki/Index_card#/media/File:LA2-katalogkort.jpg
https://commons.wikimedia.org/wiki/File:Shelves-of-file-folders.jpg
Dictionaries
• Dictionaries are Python’s most powerful data collection

• Dictionaries allow us to do fast database-like operations in Python

• Similar concepts in different programming languages

- Associative Arrays - Perl / PHP

- Properties or Map or HashMap - Java

- Property Bag - C# / .Net


Lists (Review)
>>> cards = list()
>>> cards.append(12)
>>> cards.append(3)
• We append values to the end >>> cards.append(75)
of a List and look them up by >>> print(cards)
position [12, 3, 75]
>>> print(cards[1])
• We insert values into a 3
Dictionary using a key and >>> cards[1] = cards[1] + 2
>>> print(cards)
retrieve them using a key [12, 5, 75]
Dictionaries
>>> cabinet = dict()
>>> cabinet['summer'] = 12
>>> cabinet['fall'] = 3
• We append values to the end >>> cabinet['spring'] = 75
of a List and look them up by >>> print(cabinet)
position {'summer': 12, fall': 3, spring': 75}
>>> print(cabinet['fall'])
• We insert values into a 3
Dictionary using a key and >>> cabinet['fall'] = cabinet['fall'] + 2
>>> print(cabinet)
retrieve them using a key {'summer': 12, 'fall': 5, 'spring': 75}
Comparing Lists and Dictionaries
Dictionaries are like lists except that they use keys instead of
positions to look up values

>>> lst = list() >>> ddd = dict()


>>> lst.append(21) >>> ddd['age'] = 21
>>> lst.append(183) >>> ddd['course'] = 182
>>> print(lst) >>> print(ddd)
[21, 183] {'age': 21, 'course': 182}
>>> lst[0] = 23 >>> ddd['age'] = 23
>>> print(lst) >>> print(ddd)
[23, 183] {'age': 23, 'course': 182}
Dictionary Literals (Constants)
Dictionary literals use curly braces and have key : value pairs
You can make an empty dictionary using empty curly braces

>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> print(jjj)
{'chuck': 1, 'fred': 42, 'jan': 100}
>>> ooo = { }
>>> print(ooo)
{}
>>>
Many Counters with a Dictionary
Key Value
One common use of dictionaries is
counting how often we “see” something
>>> ccc = dict()
>>> ccc['csev'] = 1
>>> ccc['cwen'] = 1
>>> print(ccc)
{'csev': 1, 'cwen': 1}
>>> ccc['cwen'] = ccc['cwen'] + 1
>>> print(ccc)
{'csev': 1, 'cwen': 2}
Dictionary Tracebacks
• It is an error to reference a key which is not in the dictionary

• We can use the in operator to see if a key is in the dictionary

>>> ccc = dict()


>>> print(ccc['csev'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'csev'
>>> 'csev' in ccc
False
When We See a New Name
When we encounter a new name, we need to add a new entry in the
dictionary and if this the second or later time we have seen the name,
we simply add one to the count in the dictionary under that name

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
if name not in counts: {'csev': 2, 'cwen': 2 , 'zqian': 1}
counts[name] = 1
else :
counts[name] = counts[name] + 1
print(counts)
The get Method for Dictionaries
The pattern of checking to see if a if name in counts:
key is already in a dictionary and x = counts[name]
assuming a default value if the key else :
is not there is so common that there x = 0
is a method called get() that does
this for us
x = counts.get(name, 0)

Default value if key does not exist


(and no Traceback). {'csev': 2, 'cwen': 2 , 'zqian': 1}
Simplified Counting with get()
We can use get() and provide a default value of zero when the key
is not yet in the dictionary - and then just add one

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print(counts)

Default {'csev': 2, 'cwen': 2 , 'zqian': 1}


Counting Pattern
counts = dict()
print('Enter a line of text:')
The general pattern to count the
line = input('')
words in a line of text is to split
words = line.split() the line into words, then loop
through the words and use a
print('Words:', words) dictionary to track the count of
print('Counting...')
each word independently.
for word in words:
counts[word] = counts.get(word,0) + 1
print('Counts', counts)
python wordcount.py
Enter a line of text:
the clown ran after the car and the car ran into the tent
and the tent fell down on the clown and the car

Words: ['the', 'clown', 'ran', 'after', 'the', 'car',


'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
'and', 'the', 'car']
Counting…

Counts {'the': 7, 'clown': 2, 'ran': 2, 'after': 1, 'car':


3, 'and': 3, 'into': 1, 'tent': 2, 'fell': 1, 'down': 1,
'on': 1}
python wordcount.py
counts = dict() Enter a line of text:
line = input('Enter a line of text:') the clown ran after the car and the car ran
words = line.split()
into the tent and the tent fell down on the
print('Words:', words) clown and the car
print('Counting...’)
Words: ['the', 'clown', 'ran', 'after', 'the', 'car',
for word in words: 'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
counts[word] = counts.get(word,0) + 1 'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
print('Counts', counts)
'and', 'the', 'car']
Counting...

Counts {'the': 7, 'clown': 2, 'ran': 2, 'after': 1,


'car': 3, 'and': 3, 'into': 1, 'tent': 2, 'fell': 1,
'down': 1, 'on': 1}
Definite Loops and Dictionaries
We can write a for loop that goes through all the entries in a
dictionary - actually it goes through all of the keys in the dictionary
and looks up the values

>>> counts = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> for key in counts:
... print(key, counts[key])
...
chuck 1
fred 42
jan 100
>>>
Retrieving Lists of Keys and Values
>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
You can get a list >>> print(list(jjj))
['chuck', 'fred', 'jan']
of keys, values, or >>> print(list(jjj.keys()))
items (both) from ['chuck', 'fred', 'jan']
a dictionary >>> print(list(jjj.values()))
[1, 42, 100]
>>> print(list(jjj.items()))
[('chuck', 1), ('fred', 42), ('jan', 100)]
>>>

What is a “tuple”? - coming soon...


Bonus: Two Iteration Variables!
We loop through the key- jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
value pairs in a for aaa,bbb in jjj.items() :
print(aaa, bbb)
dictionary using *two*
iteration variables aaa bbb
chuck 1
Each iteration, the first fred 42 [chuck] 1
jan 100
variable is the key and [fred] 42
the second variable is
the corresponding [jan] 100
value for the key
name = input('Enter file:')
handle = open(name)
python words.py
counts = dict() Enter file: words.txt
for line in handle:
words = line.split()
to 16
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
python words.py
for word,count in counts.items(): Enter file: clown.txt
if bigcount is None or count > bigcount: the 7
bigword = word
bigcount = count

print(bigword, bigcount)
Using two nested loops
Tuples
Tuples Are Like Lists
Tuples are another kind of sequence that functions much like a list
- they have elements which are indexed starting at 0
>>> x = ('Glenn', 'Sally', 'Joseph') >>> for iter in y:
>>> print(x[2]) ... print(iter)
Joseph ...
>>> y = ( 1, 9, 2 ) 1
>>> print(y) 9
(1, 9, 2) 2
>>> print(max(y)) >>>
9
but... Tuples are “immutable”
Unlike a list, once you create a tuple, you cannot alter its
contents - similar to a string

>>> x = [9, 8, 7] >>> y = 'ABC' >>> z = (5, 4, 3)


>>> x[2] = 6 >>> y[2] = 'D' >>> z[2] = 0
>>> print(x) Traceback:'str' Traceback:'tuple'
>>>[9, 8, 6] object does object does
>>> not support item not support item
Assignment Assignment
>>> >>>
Things not to do With Tuples
>>> x = (3, 2, 1)
>>> x.sort()
Traceback:
AttributeError: 'tuple' object has no attribute 'sort'
>>> x.append(5)
Traceback:
AttributeError: 'tuple' object has no attribute 'append'
>>> x.reverse()
Traceback:
AttributeError: 'tuple' object has no attribute 'reverse'
>>>
A Tale of Two Sequences
>>> l = list()
>>> dir(l)
['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']

>>> t = tuple()
>>> dir(t)
['count', 'index']
Tuples are More Efficient
• Since Python does not have to build tuple structures to be
modifiable, they are simpler and more efficient in terms of
memory use and performance than lists
• So in our program when we are making “temporary variables”
we prefer tuples over lists
Tuples and Assignment
• We can also put a tuple on the left-hand side of an assignment
statement
• We can even omit the parentheses

>>> (x, y) = (4, 'fred')


>>> print(y)
fred
>>> (a, b) = (99, 98)
>>> print(a)
99
Tuples and Dictionaries
>>> d = dict()
>>> d['csev'] = 2
>>> d['cwen'] = 4
The items() method >>> for (k,v) in d.items():
in dictionaries ... print(k, v)
returns a list of (key, ...
csev 2
value) tuples
cwen 4
>>> tups = d.items()
>>> print(tups)
dict_items([('csev', 2), ('cwen', 4)])
Tuples are Comparable
The comparison operators work with tuples and other
sequences. If the first item is equal, Python goes on to the next
element, and so on, until it finds elements that differ.
>>> (0, 1, 2) < (5, 1, 2)
True
>>> (0, 1, 2000000) < (0, 3, 4)
True
>>> ( 'Jones', 'Sally' ) < ('Jones', 'Sam')
True
>>> ( 'Jones', 'Sally') > ('Adams', 'Sam')
True
Sorting Lists of Tuples
• We can take advantage of the ability to sort a list of tuples to
get a sorted version of a dictionary
• First we sort the dictionary by the key using the items() method
and sorted() function

>>> d = {'a':10, 'c':22, 'b':1}


>>> d.items()
dict_items([('a', 10), ('c', 22), ('b', 1)])
>>> sorted(d.items())
[('a', 10), ('b', 1), ('c', 22)]
Using sorted()
>>> d = {'a':10 , 'b':1, 'c':22}
We can do this even >>> t = sorted(d.items())
more directly using the >>> t
built-in function sorted [('a', 10), ('b', 1), ('c', 22)]
that takes a sequence >>> for k, v in sorted(d.items()):
as a parameter and ... print(k, v)
returns a sorted ...
sequence a 10
b 1
c 22
Sort by Values Instead of Key
• If we could construct a >>> c = {'a':10, 'b':1, 'c':22}
>>> tmp = list()
list of tuples of the
>>> for k, v in c.items() :
form (value, key) we ... tmp.append( (v, k) )
could sort by value ...
>>> print(tmp)
• We do this with a for [(10, 'a') , (1, 'b'), (22, 'c')]
loop that creates a list >>> tmp = sorted(tmp, reverse=True)
of tuples >>> print(tmp)
[(22, 'c'), (10, 'a'), (1, 'b')]
fhand = open('romeo.txt') The top 10 most
counts = {}
for line in fhand:
common words
words = line.split()
for word in words:
counts[word] = counts.get(word, 0 ) + 1

lst = []
for key, val in counts.items():
newtup = (val, key)
lst.append(newtup)

lst = sorted(lst, reverse=True)

for val, key in lst[:10] :


print(key, val)
Even Shorter Version
>>> c = {'a':10, 'b':1, 'c':22}

>>> print( sorted( [ (v,k) for k,v in c.items() ] ) )

[(1, 'b'), (10, 'a'), (22, 'c')]

List comprehension creates a dynamic list. In this case, we


make a list of reversed tuples and then sort it.
http://wiki.python.org/moin/HowTo/Sorting
Summary
• Tuple syntax • Tuples in assignment
• Immutability statements
• Sorting dictionaries by
• Comparability
either key or value
• Sorting

You might also like