Lecture 10
Lecture 10
Dictionaries
HORT 59000
Lecture 10
Instructor: Kranthi Varala
Core data types
• Numbers
• Strings
• Lists
• Dictionaries
• Tuples
• Files
• Sets
References and dynamic typing
• Dynamic typing allows changing the type of a variable.
• A = ‘42’ now changes the apparent data type of A to an
integer.
Sample
A
String
B 42
0 1 5 9 13
1 2 6 10 14
2 3 7 11 15
3 4 8 12 16
MultiDimensional Lists
• Nested Lists need not be homogeneous.
• my2DList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,16]]
0 1 2 3
my2DList
0 1 5 9 beta
1 2 6 10 14
2 3 7 e 15
3 a cat 12 16
Arbitrary dimensional Lists
• Nested Lists need not be of the same length.
• my2DList = [[1,2,3,’a’],[5,6,7],[9,10,’e’,12,’cat’],[’beta’,14,15,16]]
0 1 2 3
my2DList
0 1 5 9 beta
1 2 6 10 14
2 3 7 e 15
3 a 12 16
cat
Arbitrary dimensional Lists
• Nested Lists can have arbitrary depth as well.
• subL = [[’p’,’q’],[‘r’,’s’]]
• my2DList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,subL]]
0 1 2 3
my2DList
0 1 5 9 beta
1 2 6 10 14
0 1
2 3 7 e 15
0 p q
3 a cat 12 1 r s
Lists as sequences of references
• myList = [’Name’,[Month,Date,Year],Address,[Home,Cell]]
int Str
int Str
int
Lists as sequences of references
• myList = [’Name’,[Month,Date,Year],Address,[Home,Cell]]
int Str
int
Lists are mutable!!
>>>subL = [[’p’,’q’],[‘r’,’s’]]
>>>myList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,subL]]
>>>myList
[[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’q’],[‘r’,’s’]
]]]
>>>subL[0][1] = ‘z’
>>>myList
[[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’z’],[‘r’,’s’]
]]]
Tuples
• Tuples are immutable general sequence
objects that allows the individual items to be of
different types.
• Equivalent to lists, except that they can’t be
changed.
Tuples
• Tuple.count(value) : Returns number of occurences of
value.
• Tuple.index(value,[start,stop]) : Returns first index of
value.
• Typically used to maintain data integrity within the
program.
Even single elements need a comma