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.
Checking if key already exists in dictionary (or list), if key does exist add value to previously existing key.
How can I check if a given key already exists in a Python dictionary? - Python - Data Science Dojo Discussions
Looping through a dictionary to check if key exists in a list of lists
How to find if multi-level key exists in dict?
Videos
Hi all,
I have a problem I just cannot wrap my head around. I don't have a lot of experience with dicitonaries if you can't tell. I've googled the everyloving goodness out of every set of words I can come up with but obviously without success.
For example, I have a spreadsheet like this (titles being animal & count);
Animal : Count
Elephant : 250 Monkey : 100 Snakes : 75 Elephant : 100
Long story short, I want the script to check if the animal already exists, and if it does, add the count to the existing animal. So elephant in this situation would come out with a count of 350.
I had sort of assumed this was best handled using a dictionary, but if not please let me know.
Thanks in advance!
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