Python Dictionaries Flashcards

1
Q

What is a python dictionary?

A

A collection which is unordered, changeable and indexed

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

What is the time complexity to access a key in a dictionary?

A

O(1) time complexity

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

How are dictionaries recorded in memory?

A

A dictionary is recorded in memory as a hash table.

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

What is a hash table?

A

A hash table is a way of doing key-value lookups. You store the values In an array, and then use a hash function to find the index of the array cell that corresponds to your key-value pair.

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

What are collisions in a hash table?

A

Different keys have the same hash value.

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

Ways to add key value pairs to a dictionary and update

A
  • myDict[‘age’] = 17

side note: o(1) time complexity

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

Set a new key value if it doesnt exist in Dictionary

A

myDict.setdefault(key, value)

if the key value already exists it does not set the value.

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

Different ways to pull values from a dictionary

A
  • myDict[key]: this will pull the value. if the key does not exist then it will return an error.
  • myDict.get(key, value): This will pull the value and return None if it does not exist. if value is set then this will be the default value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

simple way to traverse over keys in dict

A

for key in myDict:
print(key, dict[key])

O(n) time complexity

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

How to search for a value in a dictionary

A

def searchDict(dict, value):
for key in dict:
if dict[key] == value:
return key, value
return None

O(n)
O(1) for space complexity.

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

What are the 4 ways to delete a value or values from a Dictionary

A
  1. myDict.pop(key):
    removes and returns the keys value
  2. myDict.popitem():
    removes a random key
  3. myDict.clear():
    removes all the keys in the dictionary
  4. del myDict[key]:
    removes a specific key from the dictionary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

method used to create a copy of a dictionary

A

the .copy() creates a copy of the dictionaries. The original one will not change when using this

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

How do you create a dictionary using a list of keys

A

{}.fromkeys(sequence[], value])
- value is optional
- values will be None if the value is not set

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

how to get a dictionary as a list of tuples in python. Good for iteration.

A

.items() method

result example: [(key1, value1), (key2, value2)]

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

How to get all the keys in a dictionary

A

dict.keys() will return a list of keys

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

getting a list of just the values in a dictionary

A

dict.values()

17
Q

How to combine a dictionary to another dictionary

A

dict1.update(dict2)

  • updates the dict from another element
  • adds elements to the dictionary if the key does not already exist
  • if the key exists then the value is updated to the new value
  • it can also take a tuple of key value pairs
18
Q

How to determine if a key exists in the dictionary

A

key in dict

  • the in operator in a dictionary is super efficient because it is a hash table
  • O(1) time complexity
19
Q

determine if a value is in a dictionary

A

value in dict.values()

20
Q

returns only true when every value in the dictionary is true

A

all(dict)

  • if dict is empty then this will return true.
21
Q

if any element of a collection is true in a dict it will return true.

A

any(dict)

  • if it is an empty collection it will return false
22
Q

count the number of keys in a dictionary
count the number of values in a dictionary

A

: len(dict1) returns number of keys
: len(dict.values()) returns number of keys

23
Q

how to order a dictionary keys

A

sorted(myDict , reverse=False, key=None)
- reverse will reverse the order of the sort
- key will change how the sort function is sorting the keys

24
Q

What are the key differences between a list and a dictionary

A
  • Dictionary, unordered. list: ordered
  • Dictionary: Collection of key value pairs List: Collection of elements
  • Dictionary cannot have duplicate while lists can