Python list comprehension to return edge values of a list
- by mvid
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.