Dictionaires and Sets Flashcards

(34 cards)

1
Q

In dictionaries _____ does not matter

A

order

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

To create a dictionary, you place curly brackets ({}) around comma-separated key : value pairs. The simplest dictionary is an empty one, containing no keys or values at all:

A

> > > empty_dict = {}
empty_dict
{}

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

You can create a dictonary using the function _____

A

dict()
»> empty_dict = {}
»> empty_dict
{}

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

> > > pythons = {
… ‘Chapman’: ‘Graham’,
… ‘Cleese’: ‘John’,
… ‘Idle’: ‘Eric’,
… ‘Jones’: ‘Terry’,
… ‘Palin’: ‘Michael’,
… }
How to add (Gilliam, Gerry) to this list

A

> > > pythons[‘Gilliam’] = ‘Gerry’
pythons
{‘Chapman’: ‘Graham’, ‘Cleese’: ‘John’, ‘Idle’: ‘Eric’,
‘Jones’: ‘Terry’, ‘Palin’: ‘Michael’, ‘Gilliam’: ‘Gerry’}

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

Dictinoary Keys must be _____

A

unique

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

One way to key an item

A

[key]
»> some_pythons[‘John’]
‘Cleese’

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

Another way to get an item

A

get()
»> some_pythons.get(‘John’)
‘Cleese’

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

How to get all the key in a dictionary

A

keys()

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

How to get all the values in a dictionary

A

values()

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

How to get all the key-value pairs from dictonary

A

items()
»> list( signals.items() )
[(‘green’, ‘go’), (‘yellow’, ‘go faster’), (‘red’, ‘smile for the camera’)]

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

You can use the ____ funcrion to copy the keys and values from one dictonary to another

A

update()

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

Delte an item by key with ___

A

del
»> del pythons[‘Marx’]

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

If you give ____ a key and it exists in the dictionary, it returns the matching value and deletes the key-value pair. If it doesn’t exist, it raises an exception:

A

pop()

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

How to delete all item

A

clear()

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

Test for a key with ___

A

in

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

Dictionaries can be compared with the simple comparison operators ___, ____

17
Q

A set is like a dictionary wth values thrown away, leaving only ___

18
Q

Create an empty set

A

> > > empty_set = set()

19
Q

Function that you can use to create a set from a list, strnig, tuple, or dictionary (discarding any duplciate values)

A

set()
»> set( ‘letters’ )
{‘l’, ‘r’, ‘s’, ‘t’, ‘e’}

20
Q

Getting the length of a set

21
Q

How to add item to set

22
Q

How to delete an item in set

23
Q

Intersection operator

24
Q

The result of the & operator is a set that contains ____ of the items that appear in both lists that you compare. If neither of those ingredients were in contents, the & returns an empty set, which is considered ___.

25
You can also use ____ for intersection of sets
intersection()
26
Union function for sets
union()
27
Memebers of first set but no in second set
difference()
28
Check if a set is a ubset set of another set
issubset()
29
Check if all members of the second set are also in the first
issuperset()
30
____ _______ is a way to create lists using a concise syntax. It allows us to generate a new list by applying an expression to each item in an existing iterable (such as a list or range)
List Comprehesion
31
a = [2,3,4,5] res = [val ** 2 for val in a] print(res) What does this code do
[4, 9, 16, 25]
32
a = [1, 2, 3, 4, 5] res = [val for val in a if val % 2 == 0] print(res) What is the list?
[2, 4]
33
Creates a list of numbers from 0 to 9 a = [i for i in range(10)] print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
34
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] res = [val for row in mat for val in row] print(res)
[1, 2, 3, 4, 5, 6, 7, 8, 9]