0

When i executed the following python script

list= (1,2,3,4,1,2,7,8)

for number in list:
    item1= number
    item2= list[list.index(item1)+2]
    couple= item1, item2
    print couple

the goal is to link each number with the second following I obtain this result

(1, 3)
(2, 4)
(3, 1)
(4, 2)
(1, 3)
(2, 4)

(and then the index gets out of range but this is not the problem)

My question is why the number 1 in the fifth line is still coupled to the number 3 and how can i make that it is coupled to the number 7; idem for the number 2 in the sixth line that should be coupled to the number 8.

additional question what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)]

2
  • 3
    Please don't use the builtin list as a variable name! Commented Feb 5, 2011 at 21:49
  • 2
    That's not a list. A list is defined with brackets, [] abc = [1,2,3,4,1,2,7,8] Commented Feb 5, 2011 at 21:50

5 Answers 5

3

list.index returns the offset of the first occurrence of the value in the list - thus if you do [1,1,1].index(1), the answer will always be 0, even though 1 and 2 are also valid answers.

Instead, try:

from itertools import islice, izip, ifilter

mylist = [1,2,3,4,1,2,7,8]
for pair in ifilter(lambda x: x[0]==1, izip(mylist, islice(mylist, 2, None))):
    print pair

results in

(1, 3)
(1, 7)
Sign up to request clarification or add additional context in comments.

8 Comments

Note that you can write islice(mylist, 2, None).
If you're using islice why you not use izip?
what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)]
@Preys: have updated answer to include filtering on first item's value
can I use the same for a list of words for example myslist
|
1

xs.index(x) gives you the index of the first occurence of x in xs. So when you get to the second 1, .index gives you the index of the first 1.

If you need the index alongside the value, use enumerate: for i, number in enumerate(numbers): print number, numbers[i+2].

Note that I deliberately didn't use the name list. It's the name of a built-in, you shouldn't overwrite it. Also note that (..., ...) is a tuple (and therefore can't be changed), not a list (which is defined in square brackets [..., ...] and can be changed).

Comments

1

You have duplicates in the list so index always returns the first index.

Start your program with for index in range(len(list) - 1)

1 Comment

(1) -1 is wrong if you want all numbers from 0 to len(list) (range already is a half-open range) and you'd need -2 if you wanted to prevent out of bounds errors (which are not the topic of the question). (2) Use enumerate.
0

You are using .index which returns the first occurrence of number.

consider:

for number in range(len(list)):
    item1= list[number]
    item2= list[number+2]
    couple= item1, item2
    print couple

2 Comments

This results in an IndexError when number+2 goes off the end of the list.
what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)]
0
>>> zip(lst, lst[2:])
[(1, 3), (2, 4), (3, 1), (4, 2), (1, 7), (2, 8)]

To get only pairs (1, X):

>>> [(a, b) for (a, b) in zip(lst, lst[2:]) if a == 1]
[(1, 3), (1, 7)]

Recommended reading:

http://docs.python.org/tutorial/datastructures.html

http://docs.python.org/howto/functional.html

2 Comments

what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)]
@Preys: [(a, b) for (a, b) in zip(lst, lst[2:]) if a == 1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.