SlideShare a Scribd company logo
1 of 35
Welcome to the Brixton Library
Technology Initiative
(Coding for Adults)
ZCDixon@lambeth.gov.uk
BasilBibi@hotmail.com
January 30th 2016
Week 4 – Collections 2
Collections – Part 2
• This week we cover three very important
abstract data types – tuples, associative arrays
and Sets.
Collections – Tuples
• A tuple is an immutable sequence of Python
objects.
• Tuples are just like lists but cannot be
changed.
• Tuples use parentheses, whereas lists use
square brackets.
Collections – Tuples
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d”
• The empty Tuple
tup1 = ()
Collections – Tuples
• Single value tuple - you have to include a
comma
tup1 = (50,)
• Otherwise Python thinks it is an expression.
Collections – Tuples
• Any set of comma-separated, multiple objects
without identifying symbols (brackets for lists,
parentheses for tuples, etc.) default to tuples
print 'abc', -4.24e93, 18+6.6j, 'xyz'
>>> abc -4.24e+93 (18+6.6j) xyz
Collections – Tuples
Nice programming idiom:
Declare a tuple and variables at the same time
x, y = 1, 2
print "Value of x , y : ", x,y
>>> Value of x , y : 1 2
x,y = 1,2,3
ValueError: too many values to unpack
Tuples - Immutability
• You cannot update or change the values of tuple
elements. (*see partial truth)
• You are able to take portions of existing tuples to
create new tuples.
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# tup1[0] = 100
tup3 = tup1 + tup2
Tuples - Immutability
Remember list example replacing elements :
myList = [10,20,30,40,50]
myList[2:4] = ['C', 'D', 'E‘]
[10, 20, 'C', 'D', 'E', 50]
tup = (10,20,30,40,50)
tup[2:4] = ('C','D','E')
TypeError: 'tuple' object does not support item assignment
Use slices to do the same thing :
tup[:2] + ('C','D','E') + tup[4:]
(10, 20, 'C', 'D', 'E', 50)
Tuples – Immutability - Partial Truth
• There are circumstances where a tuple’s contents can change.
• If the tuple contains a List – the List’s contents can change.
a = ["apple",]
b = ["banana",]
c = ["cucumber",]
t = (a,b,c)
t
(['apple'], ['banana'], ['cucumber'])
b.append("A new banana in my immutable tuple! WAT?!")
t
(['apple'], ['banana', 'A new banana in my immutable tuple! WAT?!'],
['cucumber'])
Beware of Tuples containing
collections – those collections could
change.
Tuples – Operations Like Lists
Python Expression Results
len( (1, 2, 3) ) 3
min( (1, 2, 3) ) 1
max( (1, 2, 3) ) 3
(1, 2, 3) + (4, 5, 6) [1, 2, 3, 4, 5, 6]
('Hi!‘) * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!']
3 in (1, 2, 3) True
tuple( myList ) Convert a list to a tuple
All your favourite list
operations are also
available for Tuples.
Remember : Create
List with []
Tuple with ()
Map with {k : v}
Set with {a,b,c}
Tuple – basic operation summary
Expression Result
tup[ 3 ] 40
tup[ 0 ] 10
index=4
tup[ index ]
50
tup[ -1 ] 50
tup[ -3 ] 30
tup[ 20 ] IndexError: list index out of range
tup[ 1 : 4 ] [20, 30, 40]
tup[ : 4] [10, 20, 30, 40]
tup] [20, 40]
tup[::2] [10, 30, 50]
myList[ :: -1 ] [50,40,30,20,10]
Given tup= [10,20,30,40,50]
Collections – Associative Arrays
• In computer science, an associative
array, map, symbol table, or dictionary is
an abstract data type composed of
a collection of key-value pairs, such that each
possible key appears just once in the
collection – Wikipedia
Python Associative Array = Dict
• That means we put a value into an associative array
using a key and then get the value back using the same
key.
• Python has a dict type that is defined like this :
{ key0 : value0 , key1 : value1 , ... keyn : valuen }
• Here is a dict containing key value pairs that map
names to email addresses :
emails = { “basil” : “basilbibi@xyz.com” , “joel” : “joel@xyz.com” }
Dict Operations
• We can get the value using the associated key :
>>> emails = { 'basil' : 'basilbibi@xyz.com' , 'joel' : 'joel@xyz.com' }
>>> print emails['basil']
basilbibi@xyz.com
• We can replace a value using the same key :
>>> emails['basil'] = 'BBB@abc.com'
>>> print emails['basil']
BBB@abc.com
Dict – Adding and Removing
• We can add a new value:
>>> emails['tony'] = 'tony@abc.com'
>>> print emails['tony']
'tony@abc.com'
• We can remove an entry using del
>>> del emails['basil']
>>> print emails['basil']
KeyError: 'basil'
Can also clear the entire dict.
>>> emails.clear()
Or del it.
>>> del emails
Dict – Summary
Python Expression Results
myDict.clear()
myDict.copy()
emails = dict.fromkeys( [1,2,3], 'BBB') {1: 'BBB', 2: 'BBB', 3: 'BBB'}
emails[2] 'BBB'
emails.get(2, "Not Found") 'BBB'
Provide a value if the key is not there.
emails.get(5, "Not Found")
'Not Found'
print emails.get(5) None
emails.has_key(5) False
email.keys() [1, 2, 3]
email.values() ['BBB', 'BBB', 'BBB']
Dict – Other Rules
• Only one value per key : last one wins
• >>> emails = { 'basil' : 'basilbibi@xyz.com' , 'basil' : 'BBB@abc.com' }
• >>> print emails['basil']
• BBB@abc.com
Dict – Other Rules
• Keys must be in the Dict
• You get an error otherwise.
>>> myDodgyKey = "dodgy"
>>> emails[ myDodgyKey ] = 'no@way.com'
>>> print emails[ myDodgyKey ]
no@way.com
>>> myDodgyKey = 'HAHA'
>>> print emails[ myDodgyKey ]
KeyError: 'HAHA'
Dict – Other Rules
• Keys must be immutable – String, Number, Tuple*.
• Because if something changed the key outside of the dict then the
hashcode will not point to the correct bucket.
• Also can’t use List as a key :
>>> myDodgyKey = ['dodge']
>>> print emails[ myDodgyKey ]
TypeError: unhashable type: 'list‘
- it's complaining because there's no built-in hash function for lists (by
design), and dictionaries are implemented as hash tables –
stackoverflow.com
* Even though tuples are
immutable, they can’t be used as
dictionary keys if they contain
mutable objects.
Dict – Hashing
• What is a hash function and what are hash tables?
• Hashing is at the core of associative arrays.
• Every object in python has a hash code.
• It is a way to turn a value into a unique numeric code.
>>> hash('basil')
-1740749512
>>> hash(2)
2
>>> hash(2.0) # Python treats float and int hash as the same.
2
>>> hash (3.14159)
-1684683840
Dict – Hashing
• When you add a key value pair to a dict, the key’s hashcode is used to
store the value in the dict.
Dict – Internal Storage
• The internal storage of the dict is actually a lot of of buckets :
• When you insert a value, the hashcode is used to find a bucket to put it
into.
Dict – Buckets
• Dict starts with 8 buckets and increases in size as new elements are added.
• It’s size increases by 4 each time a threshold is reached.
• Then halves when items are removed.
Dict – Recommended Viewing
• Very good presentation on the internal workings of Python Dicts.
• http://pyvideo.org/video/276/the-mighty-dictionary-55
Python Sets
• A special kind of dict where the key is the
value.
• A set contains only one occurrence of an
object.
• You can’t add immutable objects to a set.
• Sets have mathematical operations – union,
intersection, difference, superset.
Python Sets
Python Expression Results
set( {1, 2, 3} ) { 1, 2, 3 }
set( {1,2,2,3} ) { 1, 2, 3 }
set( {1.0, 1, 2 ,3} ) {1, 2, 3} # hash (1.0) same as hash(1)
set( {1.0, "Hello", (1, 2, 3)} ) {1.0, 'Hello', (1, 2, 3) }
set( {1, 2 ,[3,4] } ) TypeError: unhashable type: 'list‘
No mutable types in a set
Python Sets
Python Expression Results
mySet .add(6) {1, 2, 3, 4, 5, 6}
mySet.update({'a','b','c'}) {'a', 1, 2, 3, 4, 5, 'c', 'b'}
mySet.remove(2) or
mySet.discard(2)
{'a', 1, 3, 4, 5, 'c', 'b'}
mySet.pop() 'a‘
myset now contains {1, 3, 4, 5, 'c', 'b'}
mySet.clear()
3 in mySet True
5 not in mySet False
Given : mySet = {1, 2, 3, 4, 5}
Python Sets - Union
Python Expression Results
A | B {1, 2, 3, 4, 5, 6, 7, 8}
A.union(B) {1, 2, 3, 4, 5, 6, 7, 8}
b.union(A) {1, 2, 3, 4, 5, 6, 7, 8}
Given : A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
Python Sets - Intersection
Python Expression Results
A & B {4, 5}
A.intersection(B) {4, 5}
B.intersection(A) {4, 5}
Given : A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
Python Sets - Difference
Python Expression Results
A - B {1, 2, 3}
A.difference(B) {1, 2, 3}
B.difference(A) {6, 7, 8}
Given : A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
Python Sets – Symmetric Difference
Python Expression Results
A ^ B {1, 2, 3, 6, 7, 8}
A.symmetric_difference(B) {1, 2, 3, 6, 7, 8}
B.symmetric_difference(A) {1, 2, 3, 6, 7, 8}
(A|B) – (A & B) {1, 2, 3, 6, 7, 8}
Given : A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
When to use Tuple, List, Dict Or Set?
• Tuple is an immutable List.
• A list keeps order, dict and set don't: when you care about order, use list.
• dict associates with each key a value, list and set just contain values.
• set items must be hashable, list don’t: if you have non-hashable items use list.
• Tuple is hashable.
• set forbids duplicates, list does not - a crucial distinction.
• Search to see if a value is in a set (or dict, for keys) is very fast .
list search it takes time proportional to the list's length.
• So, if you have hashable items, don't care about order or duplicates, and want
speedy membership checking, set is better than list.
• Stacktrace.com http://tinyurl.com/q8f2g3g
Performance of List, Dict Or Set.
• Performance of collections under different circumstanced will determine
which collection you must chose.
• Performance or ‘complexity’ of the data structure denoted by ‘Big O’
notation – There are other notations but ‘Big O’ is usually talked about by
programmers.
• https://wiki.python.org/moin/TimeComplexity
• https://justin.abrah.ms/computer-science/big-o-notation-explained.html
• http://bigocheatsheet.com/
• Complexity is not just a measure of speed, it is also a measure of how
much memory overhead the data structure consumes.
Testing Performance Of Your Code
• Obviously - consider time complexity of the data structure first unless you
are dealing with very large collections and memory is a constraint.
• Experiment with data set performance using timeit function.
• https://docs.python.org/2/library/timeit.html

More Related Content

What's hot

python chapter 1
python chapter 1python chapter 1
python chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
Raghu nath
 

What's hot (19)

Python - Lecture 3
Python - Lecture 3Python - Lecture 3
Python - Lecture 3
 
16 ruby hashes
16 ruby hashes16 ruby hashes
16 ruby hashes
 
Working with Groovy Collections
Working with Groovy CollectionsWorking with Groovy Collections
Working with Groovy Collections
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
Python - Lecture 4
Python - Lecture 4Python - Lecture 4
Python - Lecture 4
 
Inheritance & polymorphism java oop
Inheritance & polymorphism java oopInheritance & polymorphism java oop
Inheritance & polymorphism java oop
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Pa1 lists subset
Pa1 lists subsetPa1 lists subset
Pa1 lists subset
 
MySQLConf2009: Taking ActiveRecord to the Next Level
MySQLConf2009: Taking ActiveRecord to the Next LevelMySQLConf2009: Taking ActiveRecord to the Next Level
MySQLConf2009: Taking ActiveRecord to the Next Level
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
Sets
SetsSets
Sets
 
1 the ruby way
1   the ruby way1   the ruby way
1 the ruby way
 
Advanced geoprocessing with Python
Advanced geoprocessing with PythonAdvanced geoprocessing with Python
Advanced geoprocessing with Python
 
Introduction to Search Systems - ScaleConf Colombia 2017
Introduction to Search Systems - ScaleConf Colombia 2017Introduction to Search Systems - ScaleConf Colombia 2017
Introduction to Search Systems - ScaleConf Colombia 2017
 
A Search Index is Not a Database Index - Full Stack Toronto 2017
A Search Index is Not a Database Index - Full Stack Toronto 2017A Search Index is Not a Database Index - Full Stack Toronto 2017
A Search Index is Not a Database Index - Full Stack Toronto 2017
 
Dictionary part 1
Dictionary part 1Dictionary part 1
Dictionary part 1
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 

Similar to Brixton Library Technology Initiative

RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
Out Cast
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
lailoesakhan
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
lailoesakhan
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 

Similar to Brixton Library Technology Initiative (20)

An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Python
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban KGetting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
 
Brixon Library Technology Initiative
Brixon Library Technology InitiativeBrixon Library Technology Initiative
Brixon Library Technology Initiative
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
powerpoint 2-13.pptx
powerpoint 2-13.pptxpowerpoint 2-13.pptx
powerpoint 2-13.pptx
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
Tuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptxTuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptx
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
 
tupple.pptx
tupple.pptxtupple.pptx
tupple.pptx
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionaries
 
Python_Sets(1).pptx
Python_Sets(1).pptxPython_Sets(1).pptx
Python_Sets(1).pptx
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 

More from Basil Bibi (6)

Under The Covers
Under The CoversUnder The Covers
Under The Covers
 
Software Development
Software DevelopmentSoftware Development
Software Development
 
Brixton Library Technology Initiative Week2 Intro
Brixton Library Technology Initiative Week2 IntroBrixton Library Technology Initiative Week2 Intro
Brixton Library Technology Initiative Week2 Intro
 
Brixton Library Technology Initiative Week1 Recap
Brixton Library Technology Initiative Week1 RecapBrixton Library Technology Initiative Week1 Recap
Brixton Library Technology Initiative Week1 Recap
 
Brixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 RecapBrixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 Recap
 
Welcome to the Brixton Library Technology Initiative
Welcome to the Brixton Library Technology InitiativeWelcome to the Brixton Library Technology Initiative
Welcome to the Brixton Library Technology Initiative
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Brixton Library Technology Initiative

  • 1. Welcome to the Brixton Library Technology Initiative (Coding for Adults) ZCDixon@lambeth.gov.uk BasilBibi@hotmail.com January 30th 2016 Week 4 – Collections 2
  • 2. Collections – Part 2 • This week we cover three very important abstract data types – tuples, associative arrays and Sets.
  • 3. Collections – Tuples • A tuple is an immutable sequence of Python objects. • Tuples are just like lists but cannot be changed. • Tuples use parentheses, whereas lists use square brackets.
  • 4. Collections – Tuples tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d” • The empty Tuple tup1 = ()
  • 5. Collections – Tuples • Single value tuple - you have to include a comma tup1 = (50,) • Otherwise Python thinks it is an expression.
  • 6. Collections – Tuples • Any set of comma-separated, multiple objects without identifying symbols (brackets for lists, parentheses for tuples, etc.) default to tuples print 'abc', -4.24e93, 18+6.6j, 'xyz' >>> abc -4.24e+93 (18+6.6j) xyz
  • 7. Collections – Tuples Nice programming idiom: Declare a tuple and variables at the same time x, y = 1, 2 print "Value of x , y : ", x,y >>> Value of x , y : 1 2 x,y = 1,2,3 ValueError: too many values to unpack
  • 8. Tuples - Immutability • You cannot update or change the values of tuple elements. (*see partial truth) • You are able to take portions of existing tuples to create new tuples. tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # tup1[0] = 100 tup3 = tup1 + tup2
  • 9. Tuples - Immutability Remember list example replacing elements : myList = [10,20,30,40,50] myList[2:4] = ['C', 'D', 'E‘] [10, 20, 'C', 'D', 'E', 50] tup = (10,20,30,40,50) tup[2:4] = ('C','D','E') TypeError: 'tuple' object does not support item assignment Use slices to do the same thing : tup[:2] + ('C','D','E') + tup[4:] (10, 20, 'C', 'D', 'E', 50)
  • 10. Tuples – Immutability - Partial Truth • There are circumstances where a tuple’s contents can change. • If the tuple contains a List – the List’s contents can change. a = ["apple",] b = ["banana",] c = ["cucumber",] t = (a,b,c) t (['apple'], ['banana'], ['cucumber']) b.append("A new banana in my immutable tuple! WAT?!") t (['apple'], ['banana', 'A new banana in my immutable tuple! WAT?!'], ['cucumber']) Beware of Tuples containing collections – those collections could change.
  • 11. Tuples – Operations Like Lists Python Expression Results len( (1, 2, 3) ) 3 min( (1, 2, 3) ) 1 max( (1, 2, 3) ) 3 (1, 2, 3) + (4, 5, 6) [1, 2, 3, 4, 5, 6] ('Hi!‘) * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] 3 in (1, 2, 3) True tuple( myList ) Convert a list to a tuple All your favourite list operations are also available for Tuples. Remember : Create List with [] Tuple with () Map with {k : v} Set with {a,b,c}
  • 12. Tuple – basic operation summary Expression Result tup[ 3 ] 40 tup[ 0 ] 10 index=4 tup[ index ] 50 tup[ -1 ] 50 tup[ -3 ] 30 tup[ 20 ] IndexError: list index out of range tup[ 1 : 4 ] [20, 30, 40] tup[ : 4] [10, 20, 30, 40] tup] [20, 40] tup[::2] [10, 30, 50] myList[ :: -1 ] [50,40,30,20,10] Given tup= [10,20,30,40,50]
  • 13. Collections – Associative Arrays • In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of key-value pairs, such that each possible key appears just once in the collection – Wikipedia
  • 14. Python Associative Array = Dict • That means we put a value into an associative array using a key and then get the value back using the same key. • Python has a dict type that is defined like this : { key0 : value0 , key1 : value1 , ... keyn : valuen } • Here is a dict containing key value pairs that map names to email addresses : emails = { “basil” : “basilbibi@xyz.com” , “joel” : “joel@xyz.com” }
  • 15. Dict Operations • We can get the value using the associated key : >>> emails = { 'basil' : 'basilbibi@xyz.com' , 'joel' : 'joel@xyz.com' } >>> print emails['basil'] basilbibi@xyz.com • We can replace a value using the same key : >>> emails['basil'] = 'BBB@abc.com' >>> print emails['basil'] BBB@abc.com
  • 16. Dict – Adding and Removing • We can add a new value: >>> emails['tony'] = 'tony@abc.com' >>> print emails['tony'] 'tony@abc.com' • We can remove an entry using del >>> del emails['basil'] >>> print emails['basil'] KeyError: 'basil' Can also clear the entire dict. >>> emails.clear() Or del it. >>> del emails
  • 17. Dict – Summary Python Expression Results myDict.clear() myDict.copy() emails = dict.fromkeys( [1,2,3], 'BBB') {1: 'BBB', 2: 'BBB', 3: 'BBB'} emails[2] 'BBB' emails.get(2, "Not Found") 'BBB' Provide a value if the key is not there. emails.get(5, "Not Found") 'Not Found' print emails.get(5) None emails.has_key(5) False email.keys() [1, 2, 3] email.values() ['BBB', 'BBB', 'BBB']
  • 18. Dict – Other Rules • Only one value per key : last one wins • >>> emails = { 'basil' : 'basilbibi@xyz.com' , 'basil' : 'BBB@abc.com' } • >>> print emails['basil'] • BBB@abc.com
  • 19. Dict – Other Rules • Keys must be in the Dict • You get an error otherwise. >>> myDodgyKey = "dodgy" >>> emails[ myDodgyKey ] = 'no@way.com' >>> print emails[ myDodgyKey ] no@way.com >>> myDodgyKey = 'HAHA' >>> print emails[ myDodgyKey ] KeyError: 'HAHA'
  • 20. Dict – Other Rules • Keys must be immutable – String, Number, Tuple*. • Because if something changed the key outside of the dict then the hashcode will not point to the correct bucket. • Also can’t use List as a key : >>> myDodgyKey = ['dodge'] >>> print emails[ myDodgyKey ] TypeError: unhashable type: 'list‘ - it's complaining because there's no built-in hash function for lists (by design), and dictionaries are implemented as hash tables – stackoverflow.com * Even though tuples are immutable, they can’t be used as dictionary keys if they contain mutable objects.
  • 21. Dict – Hashing • What is a hash function and what are hash tables? • Hashing is at the core of associative arrays. • Every object in python has a hash code. • It is a way to turn a value into a unique numeric code. >>> hash('basil') -1740749512 >>> hash(2) 2 >>> hash(2.0) # Python treats float and int hash as the same. 2 >>> hash (3.14159) -1684683840
  • 22. Dict – Hashing • When you add a key value pair to a dict, the key’s hashcode is used to store the value in the dict.
  • 23. Dict – Internal Storage • The internal storage of the dict is actually a lot of of buckets : • When you insert a value, the hashcode is used to find a bucket to put it into.
  • 24. Dict – Buckets • Dict starts with 8 buckets and increases in size as new elements are added. • It’s size increases by 4 each time a threshold is reached. • Then halves when items are removed.
  • 25. Dict – Recommended Viewing • Very good presentation on the internal workings of Python Dicts. • http://pyvideo.org/video/276/the-mighty-dictionary-55
  • 26. Python Sets • A special kind of dict where the key is the value. • A set contains only one occurrence of an object. • You can’t add immutable objects to a set. • Sets have mathematical operations – union, intersection, difference, superset.
  • 27. Python Sets Python Expression Results set( {1, 2, 3} ) { 1, 2, 3 } set( {1,2,2,3} ) { 1, 2, 3 } set( {1.0, 1, 2 ,3} ) {1, 2, 3} # hash (1.0) same as hash(1) set( {1.0, "Hello", (1, 2, 3)} ) {1.0, 'Hello', (1, 2, 3) } set( {1, 2 ,[3,4] } ) TypeError: unhashable type: 'list‘ No mutable types in a set
  • 28. Python Sets Python Expression Results mySet .add(6) {1, 2, 3, 4, 5, 6} mySet.update({'a','b','c'}) {'a', 1, 2, 3, 4, 5, 'c', 'b'} mySet.remove(2) or mySet.discard(2) {'a', 1, 3, 4, 5, 'c', 'b'} mySet.pop() 'a‘ myset now contains {1, 3, 4, 5, 'c', 'b'} mySet.clear() 3 in mySet True 5 not in mySet False Given : mySet = {1, 2, 3, 4, 5}
  • 29. Python Sets - Union Python Expression Results A | B {1, 2, 3, 4, 5, 6, 7, 8} A.union(B) {1, 2, 3, 4, 5, 6, 7, 8} b.union(A) {1, 2, 3, 4, 5, 6, 7, 8} Given : A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8}
  • 30. Python Sets - Intersection Python Expression Results A & B {4, 5} A.intersection(B) {4, 5} B.intersection(A) {4, 5} Given : A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8}
  • 31. Python Sets - Difference Python Expression Results A - B {1, 2, 3} A.difference(B) {1, 2, 3} B.difference(A) {6, 7, 8} Given : A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8}
  • 32. Python Sets – Symmetric Difference Python Expression Results A ^ B {1, 2, 3, 6, 7, 8} A.symmetric_difference(B) {1, 2, 3, 6, 7, 8} B.symmetric_difference(A) {1, 2, 3, 6, 7, 8} (A|B) – (A & B) {1, 2, 3, 6, 7, 8} Given : A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8}
  • 33. When to use Tuple, List, Dict Or Set? • Tuple is an immutable List. • A list keeps order, dict and set don't: when you care about order, use list. • dict associates with each key a value, list and set just contain values. • set items must be hashable, list don’t: if you have non-hashable items use list. • Tuple is hashable. • set forbids duplicates, list does not - a crucial distinction. • Search to see if a value is in a set (or dict, for keys) is very fast . list search it takes time proportional to the list's length. • So, if you have hashable items, don't care about order or duplicates, and want speedy membership checking, set is better than list. • Stacktrace.com http://tinyurl.com/q8f2g3g
  • 34. Performance of List, Dict Or Set. • Performance of collections under different circumstanced will determine which collection you must chose. • Performance or ‘complexity’ of the data structure denoted by ‘Big O’ notation – There are other notations but ‘Big O’ is usually talked about by programmers. • https://wiki.python.org/moin/TimeComplexity • https://justin.abrah.ms/computer-science/big-o-notation-explained.html • http://bigocheatsheet.com/ • Complexity is not just a measure of speed, it is also a measure of how much memory overhead the data structure consumes.
  • 35. Testing Performance Of Your Code • Obviously - consider time complexity of the data structure first unless you are dealing with very large collections and memory is a constraint. • Experiment with data set performance using timeit function. • https://docs.python.org/2/library/timeit.html