473,408 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

List question

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
Mar 21 '08 #1
14 1177
On 2008-03-21 05:16:41 PM, Fr**@home.xs4all.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)

iD8DBQFH5CfQZBKKLMyvSE4RAmDdAJ4hdhYKo7sijC3hlHWGvH 23Z+kswwCfXReH
1kfE+VFM1rfHnXFIDjGGFKU=
=PLdZ
-----END PGP SIGNATURE-----

Mar 21 '08 #2
From@home writes:
if 'one' and 'two' in f:
alist.append(f)
Use:
if 'one' in f and 'two' in f: ...
Mar 21 '08 #3
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
Mar 22 '08 #4
On Mar 22, 4:38*pm, Zentrader <zentrad...@gmail.comwrote:
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

Mar 22 '08 #5
On Mar 22, 10:07 am, Arnaud Delobelle <arno...@googlemail.comwrote:
On Mar 22, 4:38 pm, Zentrader <zentrad...@gmail.comwrote:
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.
Mar 22 '08 #6
Zentrader wrote:
On Mar 22, 10:07 am, Arnaud Delobelle <arno...@googlemail.comwrote:
>On Mar 22, 4:38 pm, Zentrader <zentrad...@gmail.comwrote:
>>>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.
Mar 22 '08 #7
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.
Mar 22 '08 #8
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 '08 #9
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
about a hypothetical keyword.

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
is your native language?
--
Steven
Mar 22 '08 #10
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
Mar 23 '08 #11
On Mar 22, 8:40�pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
<zentrad...@gmail.comdeclaimed 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
� � � � wlfr...@ix.netcom.com � � � � � � wulfr...@bestiaria.com
� � � � � � � � HTTP://wlfraed.home.netcom.com/
� � � � (Bestiaria Support Staff: � � � � � � � web-a...@bestiaria.com)
� � � � � � � � HTTP://www.bestiaria.com/
Mar 23 '08 #12
On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
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.
Mar 23 '08 #13
On Mar 24, 12:19 am, Dustan <DustanGro...@gmail.comwrote:
On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
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.
Please help me understand why this is a "more ambiguous" case.

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
Mar 23 '08 #14
On Sun, 23 Mar 2008 15:55:43 -0700, John Machin wrote:
On Mar 24, 12:19 am, Dustan <DustanGro...@gmail.comwrote:
>On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
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.

Please help me understand why this is a "more ambiguous" case.
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
Mar 24 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
How do I extract a list of lists from a user defined function and print the results as strings for each list?
0
by: Brian van den Broek | last post by:
Hi all, There have been a few posts over the last month or so expressing a bit of exasperation with the "rising tide of newbie's". (Or, more accurately, the rising tide of questions from...
23
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
15
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use....
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
1
by: student2008 | last post by:
Sorry about the title its a tricky one. I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: saijin | last post by:
I'm planning to call a list of data from an XML file but when I duplicate the content inside the <data></data> it is not showing anything Here's the ActionScript 3.0 import...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.