in tests for the existence of a key in a dict:
d = {"key1": 10, "key2": 23}
if "key1" in d:
print("this will execute")
if "nonexistent key" in d:
print("this will not")
Use dict.get() to provide a default value when the key does not exist:
d = {}
for i in range(100):
key = i % 10
d[key] = d.get(key, 0) + 1
To provide a default value for every key, either use dict.setdefault() on each assignment:
d = {}
for i in range(100):
d[i % 10] = d.setdefault(i % 10, 0) + 1
...or better, use defaultdict from the collections module:
from collections import defaultdict
d = defaultdict(int)
for i in range(100):
d[i % 10] += 1
Answer from Chris B. on Stack Overflowin tests for the existence of a key in a dict:
d = {"key1": 10, "key2": 23}
if "key1" in d:
print("this will execute")
if "nonexistent key" in d:
print("this will not")
Use dict.get() to provide a default value when the key does not exist:
d = {}
for i in range(100):
key = i % 10
d[key] = d.get(key, 0) + 1
To provide a default value for every key, either use dict.setdefault() on each assignment:
d = {}
for i in range(100):
d[i % 10] = d.setdefault(i % 10, 0) + 1
...or better, use defaultdict from the collections module:
from collections import defaultdict
d = defaultdict(int)
for i in range(100):
d[i % 10] += 1
Use key in my_dict directly instead of key in my_dict.keys():
if 'key1' in my_dict:
print("blah")
else:
print("boo")
That will be much faster as it uses the dictionary's O(1) hashing as opposed to doing an O(n) linear search on a list of keys.
Videos
Hi
I'm new to coding and python, and have been given a project to work on, as part of this project I need to do the following:
I have a dictionary:
base_ref_dict = {'0000000001': '',
'0000000002': '8.2',
'0000000003': '8.3',
'0000000004': '',
'0000000005': '8.2',
'0000000006': '8.3',
'0000000007': '',
'0000000008': '8.2',
'0000000009': '8.3',
'0000000010': '8.3'}
I then have a list of lists:
MyBaseList = [['0000000001', '1.2', '3.5', '8.1', '8.0'],
['0000000002', '1.2', '3.5', '8.1', 'Not Present'],
['0000000003', '1.2', '3.5', '8.1', '8.0'],
['0000000004', '1.2', '3.5', '8.1', '8.0'],
['0000000005', '1.2', '3.5', '8.1', 'Not Present'],
['0000000006', '1.2', '3.5', '8.1', '8.0'],
['0000000008', '1.2', '3.5', '8.1', 'Not Present'],
['0000000009', '1.2', '3.5', '8.1', '8.0']]
I want to take the key from `base_ref_dict` and check if it exists in `MyBaseList`. If it does not exist, I want to remove it from `base_ref_dict`.
So the final `base_ref_dict` should look like:
base_ref_dict = {'0000000001': '',
'0000000002': '8.2',
'0000000003': '8.3',
'0000000004': '',
'0000000005': '8.2',
'0000000006': '8.3',
'0000000008': '8.2',
'0000000009': '8.3''}
I've tried the following, but I think my iterations are all messed up:
for x in base_ref_dict: #loop through dictionary
for litems in MyBaseList: #loop through base list
if x != litems[0]: #if the key is not in the Inner list
del base_ref_dict[x] #delete from dictionary
break
In the final data both `base_ref_dict` and `MyBaseList` will hold approx. 80k records, so this needs to be as time efficient as possible