Given a Dictionary:
operating_hrs = {'MONDAY': 8, 'TUESDAY': 6, 'WEDNESDAY': 5, 'THURSDAY': 8, 'FRIDAY': 3, 'SATURDAY': 5.25, 'SUNDAY': 0}
day = "FRIDAY"
I want to get the value of FRIDAY which is 3. But I got an error or empty value.
Try #1:
[v for v in operating_hrs.values() if day == v][0]
IndexError Traceback (most recent call last) in () ----> 1 [v for v in operating_hrs.values() if day == x][0]
IndexError: list index out of range
Try #2:
[v for v in operating_hrs.values() if day == v]
[ ]
Try #3:
[v[0] for v in operating_hrs.values() if day == v]
[ ]
CodePudding user response:
If you just want the value of a key in dict, simply:
operating_hrs[key]
operating_hrs.get(key) #in case the key might not exist, to avoid KeyError
You don't need list comprehension / iteration to access dict value for a key
CodePudding user response:
Like everyone else said, just use .get() or operating_hrs[day]. However, to do it iteratively:
[operating_hrs[v] for v in operating_hrs.keys() if day == v]
should work.
You're mixing up .values() and .keys().
.keys() gives you the key, which in your case is the day. .values() gives you the value, which in your case is the number.
CodePudding user response:
dictionarys operate different from lists. You cant itterate through them with indexes. If you want to itterate the old fashioned way you need to get the keys and itterate with them.
keys = operating_hrs.keys()
for key in keys:
value = operating_hrs[key]
*your code*
If you want to get key value from a different source, you can test if its a key and then get the value
if key in operating_hrs.keys():
value = operating_hrs[key]
Hope it helps!
CodePudding user response:
dictionary.values() will yield a list of values so precisely you're comparing FRIDAY
with 8,6,5...
and hence you're getting empty list. The while purpose of dictionary is to have O(1) or constant time lookup and can be simply done by operating_hrs[day]
or operating_hrs.get(day)
. You don't have a loop into the dictionary.
CodePudding user response:
You can access dictionary
item like this:
v = operating_hrs["FRIDAY"]
You're using the wrong method i.e. You're comparing the v
with the values of the dictionary not the keys of the dictionary.
operating_hrs.values()
returns the list of dictionary values.
li = list(operating_hrs.values())
>>> [8, 6, 5, 8, 3, 5.25, 0]
To get the list of dictionary keys use the keys()
function
li = list(operating_hrs.keys())
>>> ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']
items()
will return a list of tuples of items and value
li = list(operating_hrs.items())
>>> [('MONDAY', 8), ('TUESDAY', 6), ('WEDNESDAY', 5), ('THURSDAY', 8), ('FRIDAY', 3), ('SATURDAY', 5.25), ('SUNDAY', 0)]
CodePudding user response:
I'm not entirely sure what you're trying to achieve, but as others have pointed out, if you know the dictionary's key that holds the value that you want, you can simply use:
operating_hrs = {'MONDAY': 8, 'TUESDAY': 6, 'WEDNESDAY': 5, 'THURSDAY': 8, 'FRIDAY': 3, 'SATURDAY': 5.25, 'SUNDAY': 0}
operating_hrs['FRIDAY']
3
If you must use a list comprehension, then you could use something like:
day = 'FRIDAY'
hrs_friday = [h for d, h in operating_hrs.items() if d == day ][0]
print(hrs_friday)
3