Home Code How to check a value against multiple variables in Python?

How to check a value against multiple variables in Python?

Suppose you have multiple variables declared in Python and want to check if any of them equals a given value. The usual way of doing this is as follows.

x = 5
y = 12
z = 39

if x == 11 or y == 11 or z ==11:
   print("value exists in variables")
else:
   print("value does not exist in variables")

But an even more pythonic way of getting the same thing done is as follows.

if 11 in {x,y,z}:
   print("value exists in variables")
else:
   print("value does not exist in variables")

In the above code, we are creating a set containing all the variables. And then simply checking whether the value in question is a member of the set or not. This is a more readable and a more pythonic way to check whether a value exists in a given list of variables.

Check whether a value exists in a list in Python

The same notation can also be used to check whether a value exists in a list in Python.

lst =  [13,44,56,7]

if 11 in lst:
      print("value exists in list")
else:
    print("value does not exist in list")

This works well for small lists. However, if the list is very large, it would be better to convert it into a set, and then do the comparison.

lst =  [13,44,56,7]
st = set(lst)

if 11 in st:
    print("value exists in list")
else:
    print("value does not exist in list")

That will increase the speed, because in case of list, the program will have to check the value against each variable in the list. But in case of a set, it is a simple hash check, because set is implemented as a hash table in Python.

Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,269FansLike
7FollowersFollow
127FollowersFollow
378FollowersFollow

Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?

The individual human is smart, but the human species is dumb

The smart individual human has to clean up the mess created by his dumb species. Species polluted the planet, individuals are cleaning it up.

What if Dinosaurs did not go extinct 65 million years ago?

Imagine a world where dinosaurs did not go extinct, because that asteroid missed colliding with Earth. How would have life evolved then?
Content Protection by DMCA.com