Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if value x == any value in list y: python

I'm relatively new to python and want to do something like:

if value x == any value in list y:
   write x

this would operate wihtin a function, so only solutions which match a certain criteria would write to csv as they are generated

like image 461
user1665220 Avatar asked Apr 23 '26 05:04

user1665220


2 Answers

>>> x = 8
>>> if x in (1, 2, 3, 5, 8, 13, 21, 34, 55):
        print('x is an early Fibonacci number')
x is an early Fibonacci number
like image 141
poke Avatar answered Apr 24 '26 18:04

poke


You may use the in syntax:

if x in ["a", "b", "c"]
   ... do stuff

And also if you care about the number of occurances of x you may use count:

if y.count(x) == 1
  ... do stuff
like image 39
Ivaylo Strandjev Avatar answered Apr 24 '26 20:04

Ivaylo Strandjev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!