Python list comprehension to return edge values of a list
Posted
by mvid
on Stack Overflow
See other posts from Stack Overflow
or by mvid
Published on 2010-04-15T06:38:59Z
Indexed on
2010/04/15
6:43 UTC
Read the original article
Hit count: 375
If I have a list in python such as:
stuff = [1, 2, 3, 4, 5, 6, 7, 8, 9]
with length n (in this case 9) and I am interested in creating lists of length n/2 (in this case 4). I want all possible sets of n/2 values in the original list, for example:
[1, 2, 3, 4], [2, 3, 4, 5], ..., [9, 1, 2, 3]
is there some list comprehension code I could use to iterate through the list and retrieve all of those sublists? I don't care about the order of the values within the lists, I am just trying to find a clever method of generating the lists.
© Stack Overflow or respective owner