0

I want to know how do I know if there exists a specific value in a list and also to return it if exists!

a_list = [1, 2, 3, 4]
value = 1
if value in a_list:
    return value
else: return None

I know that the above code works, but I want to know whether there is a one-liner way of doing it?

1 Answers1

1

You can do it like this:

return value if value in a_list else None
bitflip
  • 3,436
  • 1
  • 3
  • 22