A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary countMap
and then loops over the items in aDict
. The first loop increases the entry in countMap
that corresponds to the current value of aDict
which is being evaluated and then increases the value in countMap
if it's already been found but if it hasn't then it sets the corresponding value in countMap
to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within countMap
to the list uni
if there values equal 1 (if the current entry is unique).
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)
A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary countMap
and then loops over the items in aDict
. The first loop increases the entry in countMap
that corresponds to the current value of aDict
which is being evaluated and then increases the value in countMap
if it's already been found but if it hasn't then it sets the corresponding value in countMap
to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within countMap
to the list uni
if there values equal 1 (if the current entry is unique).
Taken from here:
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)
A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary countMap
and then loops over the items in aDict
. The first loop increases the entry in countMap
that corresponds to the current value of aDict
which is being evaluated and then increases the value in countMap
if it's already been found but if it hasn't then it sets the corresponding value in countMap
to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within countMap
to the list uni
if there values equal 1 (if the current entry is unique).
Taken from here:
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)
A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary "countMap"countMap
and then loops over the items in "aDict"aDict
. The first loop increases the entry in "countMap"countMap
that corresponds to the current value of "aDict"aDict
which is being evaluated and then increases the value in "countMap"countMap
if it's already been found but if it hasn't then it sets the corresponding value in "countMap"countMap
to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within "countMap"countMap
to the list "uni"uni
if there values equal 1 (if the current entry is unique).
Taken from http://stackoverflow.com/questions/1032281/python-finding-keys-with-unique-values-in-a-dictionaryhere .:
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)
A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary "countMap" and then loops over the items in "aDict". The first loop increases the entry in "countMap" that corresponds to the current value of "aDict" which is being evaluated and then increases the value in "countMap" if it's already been found but if it hasn't then it sets the corresponding value in "countMap" to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within "countMap" to the list "uni" if there values equal 1 (if the current entry is unique).
Taken from http://stackoverflow.com/questions/1032281/python-finding-keys-with-unique-values-in-a-dictionary .
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)
A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary countMap
and then loops over the items in aDict
. The first loop increases the entry in countMap
that corresponds to the current value of aDict
which is being evaluated and then increases the value in countMap
if it's already been found but if it hasn't then it sets the corresponding value in countMap
to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within countMap
to the list uni
if there values equal 1 (if the current entry is unique).
Taken from here :
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)
A more efficient way (in terms of lines of code) of doing the same thing would be to use this code. The code declares a new dictionary "countMap" and then loops over the items in "aDict". The first loop increases the entry in "countMap" that corresponds to the current value of "aDict" which is being evaluated and then increases the value in "countMap" if it's already been found but if it hasn't then it sets the corresponding value in "countMap" to 0 and then increments the value (initially setting every value to 1 or if it's already been found then incrementing it). The second loop only adds values within "countMap" to the list "uni" if there values equal 1 (if the current entry is unique).
Taken from http://stackoverflow.com/questions/1032281/python-finding-keys-with-unique-values-in-a-dictionary.
aDict = {
0: 1,
1: 2,
2: 2,
4: 2,
5: 2,
6: 3,
7: 2}
countMap = {}
for v in aDict.values():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in aDict.items() if countMap[v] == 1]
print(uni)