This should go better

I have the following code, just a snippet

val = my_entry.get()
print(val)
if (val == 5) or (val == 6) or (val == 18):
	do_something(val)

in my real project are up to ten values possible.
i am still learning python, but what know till now that should go better with a list. tuple etc. My opinion is write best code as possible

Any hint ?
Regards

Rainer

An option is:

if val in (4, 5, 18):

There are other possibilities with a range or chained comparisons, depending on the particular choices of values for val, for example whether or not they are contiguous.

Let’s hear some additional suggestions from others.

2 Likes

With only ten values possible, efficiency of checking for a match is not a major issue, but if there were thousands of possible values, not contiguous, a set could be a good choice, since lookups for presence are very efficient with them. Also keep in mind that a tuple is immutable, therefore if the available choices change over time, a list, which is mutable, may be better. A set is also mutable, making adding and deleting items possible.

2 Likes

its not only the 10 elements, its anoying these or and or etc.
on the other side I need this more then once, so I think of a function like

def element_exists(element, lst):
	if element in lst:
		return True
	else:
		return False
some code
list = ("abc", "gfghh",   "DES")
if element_exists(val, list)
   do_something(val)
some code

Regards
Rainer

Your function returns the exact same value to which the condition in the if block header evaluates, namely either True or False, so you can simplify it to this:

def element_exists(element, lst):
	return element in lst

Maybe you don’t need to have the function, unless your actual code contains more complex conditions.

Now this might just be your pseudocode. However, just an fyi …

list is a Python keyword. Maybe try my_list, etc., anything other than Python keywords.

Background is 188 byte config file, each byte has a special meaning, I have a GUI with 188 Input fields, a lot of them are combox boxes, with human readable entries, so in reading the config file to the GUI the values i.e 0,1,2,3 have to be translated in n/a, on, off, toggle and so on.
In writing from the GUI these values have to be translated back numbers.

Regards

No it isn’t.

Yeah, list is a name, not a keyword. The function by that name can be used to convert something, such a str, to a list. If you overwrite that name by assigning it a value, then within the scope where that has been done, you will not be able to use that name to call the function. It might not matter in your program, but if you have a scope somewhere that contains a lot of code, you may forget which function names you reused as names of variables within that scope, and wind up with bugs. It is safest not to repurpose those function names.

1 Like

Yes, you are correct.

As @Quercus points out, it is a Python function name (I incorrectly referred to it as a Python keyword).

arb_sentence = 'Hello, everyone.  Good day to you!'
arb_sentence
'Hello, everyone.  Good day to you!'
list(arb_sentence)
['H', 'e', 'l', 'l', 'o', ',', ' ', 'e', 'v', 'e', 'r', 'y', 'o', 'n', 'e', '.', ' ', ' ', 'G', 'o', 'o', 'd', ' ', 'd', 'a', 'y', ' ', 't', 'o', ' ', 'y', 'o', 'u', '!']

or a tuple to a list conversion:

some_tuple = (1,2,3,4)
some_tuple
(1, 2, 3, 4)
list(some_tuple)
[1, 2, 3, 4]

In any case, this is what I was referring to.

thank you, belonging the code and the list, it was not real code, just for demonstration what I will do

to explain. I have a variable containing the entry number. Behind some number are the same comboxes. i.e, behind numbers 10,20,30,40 are n/a, start, stop, toggle. where 10 belonges to a device, 20 to another device and so on.
So I need the list only once for 4 devices.

Regards
Rainer