# List question

Discussion in 'Python' started by Ricky Zhou, Mar 21, 2008.

1. ### Ricky ZhouGuest

On 2008-03-21 05:16:41 PM, 4all.nl wrote:
> alist = []
> blist = [ 'one','two','one and two','one and four','five','one two']
> for f in blist:
> if 'one' and 'two' in f:
> alist.append(f)
>
> for i in alist:
> print i
>
> two
> one and two
> one two
>
>
> why is it printing the first "two"?

Look at the this line:

if 'one' and 'two' in f:

You're basically saying:

if ('one') and ('two' in f):

which is why you only get elements that contain 'two'.

Ricky

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFH5CfQZBKKLMyvSE4RAmDdAJ4hdhYKo7sijC3hlHWGvH23Z+kswwCfXReH
1kfE+VFM1rfHnXFIDjGGFKU=
=PLdZ
-----END PGP SIGNATURE-----

Ricky Zhou, Mar 21, 2008

2. ### Paul RubinGuest

From@home writes:
> if 'one' and 'two' in f:
> alist.append(f)

Use:
if 'one' in f and 'two' in f: ...

Paul Rubin, Mar 21, 2008

3. ### From@homeGuest

Hello,

I am learning python and dont quuite understand why is this happening
could someone explain?

alist = []
blist = [ 'one','two','one and two','one and four','five','one two']
for f in blist:
if 'one' and 'two' in f:
alist.append(f)

for i in alist:
print i

two
one and two
one two

why is it printing the first "two"?

tia

From@home, Mar 21, 2008
4. ### Guest

Ricky Zhou:
> Look at the this line:
> if 'one' and 'two' in f:

Very cute, it's the first time I see a bug like this. I think it's not
a common enough pattern to justify a language change, but a bit
smarter computer language may be able to do that too ;-) (it's not
easy to tell the two meanings apart, from a simple point of view the
semantics is ambiguous) Maybe an AppleScript-like language can be
designed to have such capabilities too

A more computer-friendly (and Pythonic) syntax may be ('are' is a
keyword):
if ('one', 'two') are in f: ...
That's sugar for:
if all(x in f for x in ('one', 'two')): ...

Bye,
bearophile

, Mar 21, 2008

> if ('one', 'two') are in f: ...

"are" gives me an error in Python 2.5 with a "from future import *"
statement included. What version and platform are you running. Also,
the docs don't mention it.
http://docs.python.org/ref/keywords.html

6. ### Arnaud DelobelleGuest

On Mar 22, 4:38 pm, Zentrader <> wrote:
> > if ('one', 'two') are in f: ...

>
> "are" gives me an error in Python 2.5 with a "from future import *"
> statement included.  What version and platform are you running.  Also,
> the docs don't mention it.http://docs.python.org/ref/keywords.html

That's because you have to do:

from bearophile import musings

HTH

--
Arnaud

Arnaud Delobelle, Mar 22, 2008

On Mar 22, 10:07 am, Arnaud Delobelle <> wrote:
> On Mar 22, 4:38 pm, Zentrader <> wrote:
>
> > > if ('one', 'two') are in f: ...

>
> > "are" gives me an error in Python 2.5 with a "from future import *"
> > statement included. What version and platform are you running. Also,
> > the docs don't mention it.http://docs.python.org/ref/keywords.html

>
> That's because you have to do:
>
> from bearophile import musings
>
> HTH
>
> --
> Arnaud

Thanks. I am admittedly naive and don't have any type of guard up
when on this group for people who think that type of comment makes
them intelligent/funny.

8. ### Jeff SchwabGuest

> On Mar 22, 10:07 am, Arnaud Delobelle <> wrote:
>> On Mar 22, 4:38 pm, Zentrader <> wrote:
>>
>>>> if ('one', 'two') are in f: ...
>>> "are" gives me an error in Python 2.5 with a "from future import *"
>>> statement included. What version and platform are you running. Also,
>>> the docs don't mention it.http://docs.python.org/ref/keywords.html

>> That's because you have to do:
>>
>> from bearophile import musings
>>
>> HTH
>>
>> --
>> Arnaud

>
> Thanks. I am admittedly naive and don't have any type of guard up
> when on this group for people who think that type of comment makes
> them intelligent/funny.

No one meant to laugh at you. Your naivete was not obvious. FWIW, a
sense of humor is a valuable possession in most Python-related
conversations.

Jeff Schwab, Mar 22, 2008

> No one meant to laugh at you. Your naivete was not obvious. FWIW, a
> sense of humor is a valuable possession in most Python-related
> conversations.

Perhaps someone can explain how telling something like this to the OP,
who thinks this statement will work
if 'one' and 'two' in f:
is funny and not mean. In the words of whoever it was in "Gone With
The Wind", frankly I don't give a damn (except to not mislead relative
newbies). But this has wasted enough of everyone's time.

10. ### Guest

bearophile:
> A more computer-friendly (and Pythonic) syntax may be ('are' is a keyword):

Sorry for causing confusion, I was just thinking aloud. English isn't
my first language, and sometimes I slip a bit. Replace that with:

> A more computer-friendly (and Pythonic) syntax may be ('are' is meant to be a keyword in such hypothetical situation):

Bye,
bearophile

, Mar 22, 2008
11. ### Steven D'ApranoGuest

On Sat, 22 Mar 2008 16:16:03 -0700, bearophileHUGS wrote:

> bearophile:
>> A more computer-friendly (and Pythonic) syntax may be ('are' is a
>> keyword):

>
> Sorry for causing confusion, I was just thinking aloud. English isn't my
> first language, and sometimes I slip a bit.

There is nothing wrong with what you said originally. You said "syntax
MAY be" -- that's obvious to those who know English that you are talking

Admittedly non-English speakers may not pick up on the nuances of
English, but that's not your fault.

Your English is very good, better than some native English speakers. What

--
Steven

Steven D'Aprano, Mar 22, 2008
12. ### Steven D'ApranoGuest

On Sat, 22 Mar 2008 14:55:39 -0700, Zentrader wrote:

>> No one meant to laugh at you. Your naivete was not obvious. FWIW, a
>> sense of humor is a valuable possession in most Python-related
>> conversations.

>
> Perhaps someone can explain how telling something like this to the OP,
> who thinks this statement will work
> if 'one' and 'two' in f:
> is funny and not mean.

Oh stop your whining. Nobody is making fun of anyone, or laughing at
anyone, although I'm just about ready to start laughing at *you* for your
completely unjustified conviction that people are being cruel.

Bearophile made a clearly hypothetical comment using perfectly valid and
understandable English. That you misunderstood it isn't anybody's fault
except your own. Get over it, stop trying to blame others, and move on.
You're not the victim here, and Bearophile isn't the bad guy.

--
Steven

Steven D'Aprano, Mar 22, 2008
13. ### Dennis Lee BieberGuest

On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
<> declaimed the following in comp.lang.python:

> is funny and not mean. In the words of whoever it was in "Gone With
> The Wind", frankly I don't give a damn (except to not mislead relative

Rhett Butler, I believe (I only know two characters by name, and
since the line as I recall it is "Frankly, Scarlett, I ..." it couldn't
have been her [Scarlett] speaking)
--
Wulfraed Dennis Lee Bieber KD6MOG

HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: )
HTTP://www.bestiaria.com/

Dennis Lee Bieber, Mar 23, 2008
14. ### MensanatorGuest

On Mar 22, 8:40ï¿½pm, Dennis Lee Bieber <> wrote:
> On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
> <> declaimed the following in comp.lang.python:
>
> > is funny and not mean. ï¿½In the words of whoever it was in "Gone With
> > The Wind", frankly I don't give a damn (except to not mislead relative

>
> ï¿½ ï¿½ ï¿½ ï¿½ Rhett Butler, I believe (I only know two characters by name,

You don't remember Ashley?

I guess that's only half a name.

> and
> since the line as I recall it is "Frankly, Scarlett, I ..." it couldn't
> have been her [Scarlett] speaking)
> --
> ï¿½ ï¿½ ï¿½ ï¿½ Wulfraed ï¿½ ï¿½ ï¿½ ï¿½Dennis Lee Bieber ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ KD6MOG
> ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½
> ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ HTTP://wlfraed.home.netcom.com/
> ï¿½ ï¿½ ï¿½ ï¿½ (Bestiaria Support Staff: ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ )
> ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ HTTP://www.bestiaria.com/

Mensanator, Mar 23, 2008
15. ### DustanGuest

On Mar 21, 3:57 pm, Paul Rubin <http://> wrote:
> From@home writes:
> > if 'one' and 'two' in f:
> > alist.append(f)

>
> Use:
> if 'one' in f and 'two' in f: ...

Personally, I would put parentheses around to be clearer:

if ('one' in f) and ('two' in f): ...

I'm not saying to put parentheses around everything, but in the more
ambiguous cases, it certainly helps.

Dustan, Mar 23, 2008
16. ### John MachinGuest

On Mar 24, 12:19 am, Dustan <> wrote:
> On Mar 21, 3:57 pm, Paul Rubin <http://> wrote:
>
> > From@home writes:
> > > if 'one' and 'two' in f:
> > > alist.append(f)

>
> > Use:
> > if 'one' in f and 'two' in f: ...

>
> Personally, I would put parentheses around to be clearer:
>
> if ('one' in f) and ('two' in f): ...
>
> I'm not saying to put parentheses around everything, but in the more
> ambiguous cases, it certainly helps.

To me, alternative interpretations have extremely low scores for
utility and likelihood:

(1) 'and' has higher precedence than 'in':

'one' in (f and 'two') in f # chained (x in y in z)

(2) 'and' has same precedence as 'in':

(('one' in f) and 'two') in f

Cheers,
John

John Machin, Mar 23, 2008
17. ### Steven D'ApranoGuest

On Sun, 23 Mar 2008 15:55:43 -0700, John Machin wrote:

> On Mar 24, 12:19 am, Dustan <> wrote:
>> On Mar 21, 3:57 pm, Paul Rubin <http://> wrote:
>>
>> > From@home writes:
>> > > if 'one' and 'two' in f:
>> > > alist.append(f)

>>
>> > Use:
>> > if 'one' in f and 'two' in f: ...

>>
>> Personally, I would put parentheses around to be clearer:
>>
>> if ('one' in f) and ('two' in f): ...
>>
>> I'm not saying to put parentheses around everything, but in the more
>> ambiguous cases, it certainly helps.

>

Some people don't remember, and have no interest in forcing the people
who read their code to memorize, the relative precedence of 'in' and
'and', and so don't expect folks to understand whether

'one' in f and 'two' in f

means

('one' in f) and ('two' in f)

or

('one' in (f and 'two')) in f

or something else.

This especially holds for people who have used languages where the
precedences are less sensible than they are in Python. By memory,
standard Pascal uses the second interpretation if you don't use
parentheses, which is stupid, but there you go.

> To me, alternative interpretations have extremely low scores for
> utility and likelihood:

Outside of the interactive interpreter, I prefer to know what my code
does before I write it, rather than guess that it's unlikely to be wrong.

(Or at least *think* I know what it does, which is not always the same
thing as knowing what it does.)

--
Steven

Steven D'Aprano, Mar 24, 2008