A "for" with "list" question.

Gerhard Häring gerhard.haering at gmx.de
Sat Aug 31 17:48:36 EDT 2002


* Mauro <mauro at mr-potatohead.com> [2002-08-31 14:30 -0700]:
> a = [0,1,2]
> b = [1,2,3]
> 
> 1° Print only -> 1 and 2
> Because the 1 and 2 are the only than appears in both lists.
> 
> 2° Print only -> 0 and 3
> This are the itens than are only in their lists.

Here's an example implementation:


a = [0,1,2]
b = [1,2,3]

print "items that are in both lists:"
for item in a:
    if item in b:
        print item

print
print "items that are not in both lists:"
both_lists = a + b
for item in both_lists:
    if (item not in a) or (item not in b):
        print item
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list