Thread: If i in list

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    If i in list


    I am trying to check whether the input matches either of the fruits in the list or '1'
    It works (prints "Correct!" and exits) if I type in '1' and press enter. However, when I type in 'apples' (for example, or any other fruit) it doesn't work and the loop repeats asking "Enter a fruit:".
    It obviously works when i replace
    Code:
    if fru in [fruits, '1']
    with
    Code:
     if fru in fruits or fru=='1'
    But that's not how I want to write the code. I want the code to be as short as possible

    Here is my code -
    Code:
    from sys import exit
    fruits = ['apples','oranges','bananas','mangos','strawberries']
    
    def checkfruit():
    	fru = raw_input("Enter a fruit: ")
    	if fru in [fruits, '1']:
    		print "Correct!"
    		exit(0)
    	else:
    		checkfruit()
    
    		
    checkfruit()
    Any help would be appreciated! I couldn't find "in" anywhere in Python docs, or maybe I didn't check them correctly. Just started with python yesterday hehe.
  2. #2
  3. No Profile Picture
    Brony & F/OSS Advocate
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,652
    Rep Power
    2475
    The first thing that comes to mind is to make them both sets and then check if '1' is in their union:
    python Code:
    if fru in set(fruits) | set(['1']):
        print("Correct!")
    Alternatively, you could flatten the [fruits, '1'] list by calling into the compiler.ast module, and use that:
    python Code:
    from compiler.ast import flatten
     
    # ...
     
    if fru in flatten([fruits, '1']):
        print("Correct!")
    To be quite frank though, I feel that your original if statement is the ideal one:
    python Code:
    if fru in fruits or fru=='1':
        print("Correct!")
    Keep in mind The Zen of Python, particularly "Explicit is better than implicit" and "Readability counts". In your original if statement, it's very clear to see what the conditions are; whereas in the other two, even if it does the check against only one list (or set), it takes a bit more thought to see what the conditions are and when it would be true/false.
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Originally Posted by codergeek42
    The first thing that comes to mind is to make them both sets and then check if '1' is in their union:
    Well sure, but it’s just as easy to use list concatenation:

    Code:
    if fru in fruits + ['1']:
    Alternatively, you could flatten the [fruits, '1'] list by calling into the compiler.ast module, and use that:
    I have no idea where you got that compiler.ast thingy–seems not be present in my Python installations, nor is it documented in docs.python.org!
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Thumbs up


    Thanks a lot for your help guys. That was really well elaborated
    Gotta get play around with lists for a while
  8. #5
  9. No Profile Picture
    Brony & F/OSS Advocate
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,652
    Rep Power
    2475
    Originally Posted by SuperOscar
    Well sure, but it’s just as easy to use list concatenation: [..]
    I forgot you could do that with lists...Derp. Thanks.


    I have no idea where you got that compiler.ast thingy–seems not be present in my Python installations, nor is it documented in docs.python.org!
    Hmm...that's odd. I did a quick Google search and found it mentioned on some other forums. On my Fedora system, it's part of the core install, so...
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )

IMN logo majestic logo threadwatch logo seochat tools logo