1

Is there an easier way to do the following without me having to write out outputbodypixel == everytime? Ideally I want to pull the data from a list where I can just add #ebebeb, #ececec, #212121.

if(outputbodypixel == "#356044" or outputbodypixel == "#22402b" or
   outputbodypixel == "#213f2c" or outputbodypixel == "#356043" or
   outputbodypixel == "#22402c" or outputbodypixel == "#346044"):
    output_body = "green"
elif(outputbodypixel == "#7c3d15" or outputbodypixel == "#493613" or
     outputbodypixel == "#4a3612" or outputbodypixel == "#6a553e" or
     outputbodypixel == "#785735" or outputbodypixel == "#5e4b37" or
     outputbodypixel == "#6a553e" or outputbodypixel == "#86623c" or
     outputbodypixel == "#8b4f0d" or outputbodypixel == "#7c3d14" or
     outputbodypixel == "#6a553d" or outputbodypixel == "#7c3d14" or
     outputbodypixel == "#6a553d" or outputbodypixel == "#7c3d14" or
     outputbodypixel == "#87623c" or outputbodypixel == "#6e3612" or
     outputbodypixel == "#87613c"):
    output_body = "brown"
elif(outputbodypixel == "#8b8b8a" or outputbodypixel == "#8b8b8b" or
     outputbodypixel == "#8b8a8b"):
    output_body = "silver"
else:
    output_body = "NOT DEFINED"

I'm using Python 3.x.

martineau
  • 119,623
  • 25
  • 170
  • 301
Namenone
  • 344
  • 2
  • 5
  • 19
  • 1
    Put all the possible colors (conditions) in a list, then use `if outputbodypixel in color_list:` – MattDMo Sep 05 '21 at 21:54

3 Answers3

5

Simply use in with a tuple (or a list if you already have one for each case) of all possible values per each condition, it's exactly the same as asking if outputbodypixel is one value or the other or the other etc.

if outputbodypixel in ("#356044", "#22402b", "#213f2c", "#356043", "#22402c", "#346044"):
    output_body = "green"
elif outputbodypixel in ("#7c3d15", "#493613", "#4a3612", "#6a553e", "#785735", "#5e4b37", "#6a553e", "#86623c", "#8b4f0d", "#7c3d14", "#6a553d", "#7c3d14", "#6a553d", "#7c3d14", "#87623c", "#6e3612", "#87613c"):
    output_body = "brown"
elif outputbodypixel in ("#8b8b8a", "#8b8b8b", "#8b8a8b"):
    output_body = "silver"
else:
    output_body = "NOT DEFINED"
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    Thanks so much! Had no idea you could do it this way, exactly what i was looking for! – Namenone Sep 05 '21 at 22:22
  • 1
    My pleasure! if the lists are very big and you need an extra performance boost, consider using sets instead, which have `O(1)` cost for the `in` operation - as opposed to the `O(n)` cost for tuples or lists. – Óscar López Sep 05 '21 at 22:27
2

For a solution that also has solid performance, you could use a dict or a defaultdict for this. If you use a defaultdict, you can pass a source dict into the constructor as follows.

from collections import defaultdict


pixel = defaultdict(lambda: 'NOT DEFINED', {
    '#356044': 'green',
    '#22402b': 'green',
    '#7c3d15': 'brown',
    '#8b8b8a': 'silver',
    '#8b8b8b': 'silver'
})

pixel['#356044']
# green

pixel['#8b8b8b']
# silver

pixel['#something-else']
# NOT DEFINED
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
-3

You could create a function like this:

def output(value):
    return f"outputbodypixel == #{value}"

#You can call the function like this:

if output(12345) or output(5678):
    pass

This is just so you don't type every time the "outputbodypixel". And if you want to grab the value from a list, just make a for loop inside the created function that iterates through the values of the list. Hope this helps :D

  • 3
    This is plain wrong, it doesn't answer OPs question. What makes you think that the _string_ `"outputbodypixel == somevalue"` is the same as evaluating the expression `outputbodypixel == somevalue`? – Óscar López Sep 05 '21 at 22:21