471,330 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

List search

I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system

I'm looking to return the list item that just has 'x11'. How can I
structure my search term so that this output is returned?
Sep 28 '07 #1
5 986
On Fri, 28 Sep, Kevin Walzer wrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line
Just compare for equality:

if line == 'x11':

or

print "\n".join(x for x in mylist if x == 'x11')

--
Stefan Bellon
Sep 28 '07 #2
Kevin Walzer wrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system

I'm looking to return the list item that just has 'x11'. How can I
structure my search term so that this output is returned?
line == 'x11'

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Sep 28 '07 #3
On 28/09/2007, Kevin Walzer <kw@codebykevin.comwrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system
That output is correct, you are asking your script to print any list
item containing x11 when what you actually wanted was a list item that
is the string 'x11'

mylist = ['x11', 'x11-wm', 'x11-system']
for item in mylist:
if item == 'x11':
print line

If there is only ever one 'x11' in the list you could also consider

print mylist.index('x11')

and

print mylist[mylist.index('x11')]

Also, before iterating the whole list check that 'x11' exists

if 'x11' in mylist:
do stuff

and list comprehesions

print [x for x in mylist if x == 'x11']

HTH :)

Tim Williams
Sep 28 '07 #4
Robert Kern wrote:
line == 'x11'
D'oh! That was simple, wasn't it? *smacks head*

That did the trick. Thanks!

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Sep 28 '07 #5
Kevin Walzer wrote:
I'm having a problem with searching a list. Here's my code:

mylist = ['x11', 'x11-wm', 'x11-system']

for line in mylist:
if 'x11' in line:
print line

This results in the following output:

x11
x11-wm
x11-system

I'm looking to return the list item that just has 'x11'.
Why do you want to have an item you already know?

If you just wanted to check *if* the string "x11" is in the list,
you'd use

if "x11" in mylist:

Regards,
Björn

--
BOFH excuse #221:

The mainframe needs to rest. It's getting old, you know.

Sep 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
24 posts views Thread by Robin Cole | last post: by
10 posts views Thread by free2cric | last post: by
2 posts views Thread by Mark Drummond | last post: by
6 posts views Thread by Henrik Goldman | last post: by
reply views Thread by rosydwin | last post: by

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.