0

Set-up

I have a dictionary variable tags_dict.

I have an if condition that looks like,

    if 'seat_height_min' in tags_dict and 'seat_height_max' in tags_dict and 'material_wheels' in tags_dict:
        #do something

Question

How do I write this simpler?

I'd love to do something along the lines of,

if all('seat_height_min','seat_height_max','material_wheels') in tags_dict:
    # do something

but this doesn't work.

Is there a function that does something like the above?

LucSpan
  • 1,831
  • 6
  • 31
  • 66
  • 1
    Maybe something like `if all([x in tags_dict for x in ['seat_height_min', 'seat_height_max', 'material_wheels']]):` Though be warned that this won't perform any short circuiting if any of the boolean expressions are false – byxor Jun 01 '22 at 15:09
  • 2
    @byxor Omit the outer `[]`, just `all(x in ...)` is more efficient, and then it *will* short-circuit. – deceze Jun 01 '22 at 15:11

2 Answers2

1

You could iterate through a list of your strings as below

if all(i in d for i in ['seat_height_min','seat_height_max','material_wheels']):
  #do something
EoinS
  • 5,405
  • 1
  • 19
  • 32
1
d = {'a':1,'b':2,'c':3}
all([x in d for x in ['a','b','c']])

the code above returns true

Amir
  • 129
  • 4