[Tutor] Printing two elements in a list

Bob Gailer bgailer at alum.rpi.edu
Tue Dec 7 18:29:51 CET 2004


At 08:22 AM 12/7/2004, kumar s wrote:
>Dear group,
>  I have two lists names x and seq.
>
>I am trying to find element of x in element of seq. I
>find them. However, I want to print element in seq
>that contains element of x and also the next element
>in seq.
>
>
>So I tried this piece of code and get and error that
>str and int cannot be concatenated
> >>> for ele1 in x:
>         for ele2 in seq:
>                 if ele1 in ele2:
>                         print (seq[ele1+1])

The problem here is that ele1 is a string, not an index into the list. 
There are a couple ways to fix this.

match = False
for ele1 in x:
     for ele2 in seq:
         if match:
             print ele2
             match = False
         if ele1 in ele2:
             print ele2
             match = True
OR

for ele1 in x:
     for index, ele2 in enumerate(seq):
         if ele1 in ele2:
             print ele2, seq[index+1]

>[snip]

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 



More information about the Tutor mailing list