Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104886

Re: Use of Lists, Tupples, or Sets in IF statement.

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Use of Lists, Tupples, or Sets in IF statement.
Date 2016-03-15 11:33 +1100
Message-ID <mailman.142.1458001996.12893.python-list@python.org> (permalink)
References <fcd34352-44d2-4912-b76c-7b3860c4fe1e@googlegroups.com>

Show all headers | View raw


On Tue, Mar 15, 2016 at 11:26 AM,  <jj0gen0info@gmail.com> wrote:
> In Python is it possible to comparison-equate a variable to a List, Tupple, or Set and have it return True if the contents of the variable matches an element in the List, Tupple, or Set.
>
> E.g.
>
> x = "apple"
>
> x-list = ["apple", "banana", "peach"]
>
> If x == x-list:
>     print('Comparison is True')
> else:
>     print('Comparison is False')

Yep! What you're looking for is the "membership" operator. It's spelled "in":

>>> x = "apple"
>>> x_list = ["apple", "banana", "peach"]
>>>
>>> if x in x_list:
...     print("That is a fruit.")
... else:
...     print("That is not a fruit.")
...
That is a fruit.
>>> x = "jump"
>>> if x in x_list:
...     print("That is a fruit.")
... else:
...     print("You must be Chell.")
...
You must be Chell.


ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Use of Lists, Tupples, or Sets in IF statement. jj0gen0info@gmail.com - 2016-03-14 17:26 -0700
  Re: Use of Lists, Tupples, or Sets in IF statement. Joel Goldstick <joel.goldstick@gmail.com> - 2016-03-14 20:31 -0400
  Re: Use of Lists, Tupples, or Sets in IF statement. Chris Angelico <rosuav@gmail.com> - 2016-03-15 11:33 +1100
  Re: Use of Lists, Tupples, or Sets in IF statement. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 00:38 +0000
    Re: Use of Lists, Tupples, or Sets in IF statement. jj0gen0info@gmail.com - 2016-03-14 18:32 -0700
  Re: Use of Lists, Tupples, or Sets in IF statement. jj0gen0info@gmail.com - 2016-03-14 18:24 -0700
  Re: Use of Lists, Tupples, or Sets in IF statement. Steven D'Aprano <steve@pearwood.info> - 2016-03-15 13:10 +1100
  Re: Use of Lists, Tupples, or Sets in IF statement. Rustom Mody <rustompmody@gmail.com> - 2016-03-14 23:30 -0700
    Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Peter Otten <__peter__@web.de> - 2016-03-15 09:29 +0100
      Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Rustom Mody <rustompmody@gmail.com> - 2016-03-15 04:08 -0700
        Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Peter Otten <__peter__@web.de> - 2016-03-15 18:34 +0100
          Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Rustom Mody <rustompmody@gmail.com> - 2016-03-15 19:54 -0700

csiph-web