0% found this document useful (0 votes)
11 views

Python Collections

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)
11 views

Python Collections

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/ 1

join Google Group

Collections in python

 Lists – mutable (can change), ordered collection


o Defined using square brackets and commas
o E.g.: ages=[21, 24, 40]
o Index starts with 0 not 1.
o Print(age[2])
 40
o Heterogenous – can include numbers, string, etc
o Strings are really lists of characters
o Slicing
o Append, extend
o Del fruit = no variable
o Fruit.clear = no entries in variable
o Multidimensional lists (lists of lists / nested lists). Useful to store matrices
o List comprehensions
 Tuples are immutable (cannot change)
 Sets.
o Set of green, blue, red is the same as the set red, blue, green
o Do not contain duplicates.
o If you put a list in a set, duplicates are removed
o Unordered list. So e.g. for a genome you need to use a list because you need
it in order
 Dictionaries
o Data structure which has a key and a value
o Very fast to read and write
o Use curly brackets
o Example:
If you have a list with names. The fastest would be the 1st on list and slowest would be the
last on list. Average time is n/2.
If you use a function e.g. number of letters in each name – you can create buckets. When
you search for a record, instead of going linearly, you will use function of what you’re
searching and you go directly to that bucket.
In a list you only have 1 best case. If there are 6 buckets, there are 6 best cases.
This is called a hash function. You want many shallow buckets. It has to be deterministic –
same input always gets the same output. Average time is log n

You might also like