The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
If i in list
Discuss If i in list in the Python Programming forum on Dev Shed. If i in list Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 18th, 2013, 03:25 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 29 m 15 sec
Reputation 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.
|

March 18th, 2013, 04:22 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
The first thing that comes to mind is to make them both sets and then check if '1' is in their union:
python Code:
Original
- 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:
Original
- 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:
Original
- 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.
|

March 19th, 2013, 02:59 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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']:
Quote: | 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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

March 19th, 2013, 03:44 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 2
Time spent in forums: 29 m 15 sec
Reputation Power: 0
|
|
Thanks a lot for your help guys. That was really well elaborated
Gotta get play around with lists for a while 
|

March 23rd, 2013, 02:50 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
Quote: | 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.
Quote: | 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... 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|