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")Code language: Python (python)

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")Code language: Python (python)

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")Code language: Python (python)

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")Code language: Python (python)

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

Download HitXP Mobile App

Get it on Google Play
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

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Power of Democracy: How Free Societies Foster Innovation in Science and Technology

Democracy allows freedom of speech and thought, boosting creativity and innovation. This leads to scientific breakthroughs and technological advancements.

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Latest Music Notations

Jai Shri Ram – Adipurush – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Jai Shri Ram from the 2023 Hindi movie Adipurush in Western and Indian Notations.

Sojugada Soojumallige – Garuda Gamana Vrishabha Vahana – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Sojugada Soojumallige from the 2021 Kannada movie Garuda Gamana Vrishabha Vahana in Western and Indian Notations.

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.
Content Protection by DMCA.com