# [Tutor] Printing two elements in a list

Kent Johnson kent37 at tds.net
Tue Dec 7 16:59:53 CET 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])

You are confusing the elements themselves with their indices. The problem here is that ele1 is a
string - an element of x - not a number, which is what you need for an index.

>>>>for ele1 in x:
>
> 	for ele2 in seq:
> 		if ele2 in range(len(seq)):
> 			if ele1 in ele2:
> 				print seq[ele2+1]

Now ele2 is a number, so 'if ele1 in ele2' is never true.

> 3. TRIAL 3:
> I just asked to print the element in seq that matched
> element 1 in X.  It prints only that element, however
> I want to print the next element too and I cannot get
> it.
>
>>>>for ele1 in x:
>
> 	for ele2 in seq:
> 		if ele1 in ele2:
> 			print ele2

The enumerate function is useful here. enumerate(seq) returns a sequence of (index, element) pairs.
So you could write

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

Kent
```