Tuples & Dictionaries (UCSD - Wk 2) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

how do you pronounce tuple

A

too pel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a tuple and a list?

A

A tuple is immutable (you can’t change any of the values)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference in how you instantiate a tuple and a list?

A

list = [x, y]; tuple = (x, y); can also instantiate a tuple with a list with func: tupleVar = tuple(list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is it important that it’s immutable for parallel computing?

A

Don’t need to account for multiple threads / people using diff data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is the immumtability important for using tuples with dictionaries?

A

Tuples can be used as keys in dict. since values don’t change, can org on initial value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the python equivalent of a map?

A

Dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we think about the structure of a dictionary?

A

It’s like an index: Key Value Pairs, where Key is immutable (value or tuple)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What kinds of objects can you store in the value of a dictionary?

A

Any. ints, lists, even other dictionaries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do you initialize a dictionary?

A

dict = {key1:value1, key2, value2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how do you initialize a dict with a tuple as a key

A

dict = {(key1A, key1B): value1, … }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

lookup value for key1 in dict

A

dict[ key1 ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you add to a dictionary

A

create a new key / value pair. If key already in use, over-writes value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

is there a fixed order for dictionaries?

A

no. (one of the reasons they’re fast to lookup)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens if you try to retrieve a value for a dict where the key does not exist?

A

get a run-time error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s a safer way to retrieve an element from a dictionary if you’re not sure if the key exists

A

x = dict.get(key); Returns None if key non-existent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Test to see if a key is in a dictionary

A

key1 in dict; returns bool

17
Q

how to retrieve and delete value with key1 in dict

A

dict.pop(key1)

18
Q

how to just delete value with key from dict

A

del dict[key1]

19
Q

iterate over keys in a dictionary

A

for i in dict: print(i)

20
Q

iterate over key values in a dictionary

A

for key, value in dict.items(): print(key, “:”, value)

21
Q

can you change a dict while iterating through it?

A

no.

22
Q

how do you remove a set of items from a dict with iteration?

A

iterate through appending keys to remove to a new list, then iterate through the list, popping items from orig dict

23
Q

how to build a list with squares from 1 to 10 (eg of “comprehension”)

A

list = [i**2 for i in range (1,11)] > can use an iteration and values here

24
Q

How to create a list with random numbers (after import random package)

A

list = [random.randint(0,5) for i in range (x,y)]

25
Q

how do you build a dictionary using comprehension

A

dict = {key : value for i in range (0,10)}

26
Q

What are some distinguishing features of sets

A

unique elements, un-ordered (allowing to be very fast), and useful math operations

27
Q

how do you initialize a set

A

setVar = set([‘blue’, ‘green’, ‘red’]) or set(list)

28
Q

how do you add an element to a set

A

setVar.add(value)&raquo_space; if you add an element that already exists, no change (no error)

29
Q

how do you discard an element from a set

A

setVar.discard(value)&raquo_space; if not there, does nothing and no error

30
Q

return all elements that in two sets

A

eitherSetVar = set1.union(set2)&raquo_space; just returns all unique values

31
Q

return elements that are common to both sets

A

bothSetVar = set1.intersect(set2)

32
Q

Alternatives to union and intersect methods

A

OR ( | ) and AND ( & ) operators&raquo_space; set1 | set2; set1 & set2