0
def get_pairs(some_list, limit):
    min = 2
    pair_dict = collections.defaultdict(list)

    for x in range(min, limit+1):
        temp_list = list(itertools.combinations(some_list, x))
        pair_dict[x].append(temp_list)

    return pair_dict

z = get_pairs([1, 2, 3, 4], 4)
for key, value in z.items():
    print("Key: {}, Value: {}".format(key, value))

Output:
Key: 2, Value: [[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]]
Key: 3, Value: [[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]]
Key: 4, Value: [[(1, 2, 3, 4)]]

But What I want is something like below

Expected output:
Key: 2, Value: [[(1, 2), (3, 4)]]
Key: 3, Value:[[(1, 2, 3)]]
Key: 4, Value: [[(1, 2, 3, 4)]]

Prabu Raj
  • 577
  • 1
  • 6
  • 11
  • [splitting-a-list-into-n-parts-of-approximately-equal-lengt](https://stackoverflow.com/questions/2130016/splitting-a-list-into-n-parts-of-approximately-equal-length) – Patrick Artner Apr 29 '20 at 06:31
  • [how-do-you-split-a-list-into-evenly-sized-chunks](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Patrick Artner Apr 29 '20 at 06:31
  • I originally duped to those, but removed the dupe again as your output is a dict not a list. Its only a 1-step from one to the other, but this kind of transfere thinking needed leads to ppl unduping the question anyhow. – Patrick Artner Apr 29 '20 at 06:33
  • why is `(2, 3, 4)` not in the `key = 3` list? – Tomerikoo Apr 29 '20 at 06:49

1 Answers1

1

If there should not be any overlap, don't even bother with combinations (they just overproduce and need to be filtered). Just go with consecutive slices:

def get_pairs(some_list, limit):
    pair_dict = {}

    for x in range(2, limit+1):
        pair_dict[x] = [(some_list[i:i+x]) for i in range(0, len(some_list)-x+1, x)]

    return pair_dict
user2390182
  • 72,016
  • 6
  • 67
  • 89