-2

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]

[ ]

iBoon
  • 13
  • 4

6 Answers6

1

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

Ash Nazg
  • 514
  • 3
  • 6
  • 14
0

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']
Sidharth Mudgil
  • 1,293
  • 8
  • 25
0

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.

Craze XD
  • 110
  • 8
0

Dictionaries operate different from lists. You can't iterate over them with indexes.

If you want to iterate the old fashioned way, you need to get the keys and then iterate over those.

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Almos
  • 129
  • 7
  • That is incorrect in Python3. To iterate over a dictionary's keys, you simply have to use a for loop: `for i in operating_hrs`. If you want to iterate over the keys and values at the same time use (in Python3): `for d, h in operating_hrs.items()`. See [the documentation](https://docs.python.org/3/tutorial/datastructures.html#looping-techniques) – Leonardo Jun 27 '22 at 06:56
  • Yes you can also use a normal for loop, but since its a questions about the understanding of dicts, it would be better to do simple steps – Almos Jun 27 '22 at 07:00
  • You said `If you want to itterate the old fashioned way you need to get the keys and itterate with them`. That's not correct, you don't need to explicitly call `.keys()` to iterate over a dictionary's keys. Not calling `.keys()` is simpler. – Leonardo Jun 27 '22 at 07:06
0

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.

Anonymous
  • 335
  • 1
  • 2
  • 17
0

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
Leonardo
  • 1,533
  • 17
  • 28