### Thread: Comparing elements of lists

1. #### Comparing elements of lists

greets,

is it possible to compare elements of one list,against elements of another list?for example:

light=['white', 'grey', 'silver']
dark=['black', 'blue', 'brown']
both=light+dark

test1=['white', 'black']
test2=['white']
test3=['black']

>>light in both
false

>> test1 in both
false

>> test2 and test3 in both
true

i am wondering,why it returns false when i check if "test1" is in the list of "both",but it returns true when i check if "test2" and "test3" are in the list "both"?Both elements of "test1" are in the "both" list,but it returns false,why?can anyone please explain?eventually i would like to be able to take the elements of a list,and see if any of those elements are in another list.
Last edited by Boceifus; September 20th, 2004 at 01:40 PM. Reason: fixed typo :)
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2004
Location
Germany
Posts
11
Rep Power
0
hi,
first of all, what is "black" in line 3? i think you mean "dark"!?
i always get "false" (changed line 3)
Code:
```light = ['white', 'grey', 'silver']
dark = ['black', 'blue', 'brown']
both= light + dark

test1=['white', 'black']
test2=['white']
test3=['black']

if light in both:
print "true"
else:
print "false"

if test1 in both:
print "true"
else:
print "false"

if test2 and test3 in both:
print "true"
else:
print "false"```
perhaps you should take a look here, where list comparison was already discussed
hi,
first of all, what is "black" in line 3? i think you mean "dark"!?

perhaps you should take a look here, where list comparison was already discussed
hehe,yes i meant to put "both=light+dark",thank you.and i did come upon that thread when i did a search,but that finds the differences.

what i want,is to check if the elements of a given list(test1), to see if one or more of those elements exsist in another list(the "both" list).then i will need to print out only those elements that match,and not the entire contents of either list.is this possible?
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2004
Location
Germany
Posts
11
Rep Power
0
yeah, check out DevCoach's post again
Code:
```list1 = ['testName', 'test@email.com', '123 street', '/usr/local/bin/myScript.sh', 'None']
list2 = ['testName', 'test@email.com', '456 street', '/usr/local/bin/orTest9d.sh', 'None']

print sets.Set(list1) & sets.Set(list2)```
prints
"Set(['None', 'test@email.com', 'testName'])"

if you want it without the "Set" and brackets, use the following form:
Code:
```import sets

list1 = ['testName', 'test@email.com', '123 street', '/usr/local/bin/myScript.sh', 'None']
list2 = ['testName', 'test@email.com', '456 street', '/usr/local/bin/orTest9d.sh', 'None']

result = sets.Set(list1) & sets.Set(list2)
for i in result:
print i```
for your problem (short example ):
Code:
```import sets

light = ['white', 'grey', 'silver']
test1 = ['white', 'black']

result = sets.Set(light) & sets.Set(test1)
for i in result:
print i```
hope this is for your problem, otherwise i am to stupid
5. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
London, England
Posts
1,585
Rep Power
1373
You are searching for a list inside another list which will always be false since the list only has strings in it. e.g.

Code:
```>>> ['foo'] in ['foo', 'bar', 'baz']
False
>>> 'foo' in ['foo', 'bar', 'baz']
True
>>> ['foo'] in [ ['foo'], 'bar', 'baz']
True```
Take a look at the sets module. This will do what you really want.

Dave - The Developers' Coach
6. Sorry took me so long to reply,but once I got your suggestions,I was happily playing around with sets.Sets are excatly what I wanted,thank you.

for your problem (short example ):
Code:
```import sets

light = ['white', 'grey', 'silver']
test1 = ['white', 'black']

result = sets.Set(light) & sets.Set(test1)
for i in result:
print i```
hope this is for your problem, otherwise i am to stupid