Posts: 6,920 Threads: 22 Joined: Feb 2020 Jul-15-2024, 03:21 PM (This post was last modified: Jul-15-2024, 04:00 PM by deanhystad.) Provide some example data and corresponding example output. I think there's a lot of confusion about what you are trying to do. For example, why do you have multiple lists of items? Normally I would expect a list of bins/compartments and a list of items. Binning would sort through the list of items and place each item in the correct bin. Are the item lists in your list_of_items pre-binned, i.e. do all items in a list fit in one container? Changing my previous post to work with item number/text tuples. list_of_items = [ [(171, "text"), (172, "text"), (173, "text")], [(567, "text"), (568, "text"), (569, "text")], ] list_of_compartments = [(472, 588), (110, 260),] def get_items(compartment): min_, max_ = compartment for items in list_of_items: if min_ <= items[0][0] <= max_: return items return items compartments = {c: get_items(c) for c in sorted(list_of_compartments)} print(*compartments.items(), sep="\n")Output: ((110, 260), [(171, 'text'), (172, 'text'), (173, 'text')]) ((472, 588), [(567, 'text'), (568, 'text'), (569, 'text')]) Posts: 7 Threads: 1 Joined: Jul 2024 This is a part of list list_of_items = [[171, 'Herbert'], [172, 'Williams'], [173, '34'], [174, 'brown'],[175, '{medium}'], [176, 'FGO'], [177, '{53227650}'], [567, 'Edgar'], [568, 'Davis'], [569, '21'], [570, 'green'], [571, '{medium}'], [572, 'JRV'],[ 573, '{42965307}']] Posts: 1,144 Threads: 114 Joined: Sep 2019 Are you wanting the numbers grouped together? Posts: 7 Threads: 1 Joined: Jul 2024 Jul-15-2024, 05:45 PM (This post was last modified: Jul-15-2024, 05:54 PM by przonak007.) Yes, I would like the numbers to be grouped like this The compartment: (110, 260) 171, Herbert 172, Williams 173, 34 ... ... The compartment: (472, 588) 567, Edgar 568, Davis 569, 21 ..... .... Posts: 1,144 Threads: 114 Joined: Sep 2019 The example I posted should give that. Or one of the other examples. Posts: 7 Threads: 1 Joined: Jul 2024 Your code on the post works fine but only with numbers. When I added text as second parameter then the code stopped working. I edited my last post. I added how should be like gruped with new data Posts: 6,920 Threads: 22 Joined: Feb 2020 This assumes the items are not already grouped together in compartments. items = [ (173, "text"), (3, "text"), (171, "text"), (567, "text"), (172, "text"), (600, "text"), (569, "text"), (568, "text"), ] bins = {(110, 260): [], (472, 588): []} def bin_items(items, bins): bins = {b: [] for b in bins} default = [] for item in items: for (min_, max_), bin_ in bins.items(): if min_ <= item[0] <= max_: bin_.append(item) break else: default.append(item) if default: bins["Misfits"] = default return bins bins = bin_items(items, ((110, 260), (472, 588))) for b, items in bins.items(): print("The compartment", b) for i in sorted(items): print(*i) print()Output: The compartment (110, 260) 171 text 172 text 173 text The compartment (472, 588) 567 text 568 text 569 text The compartment Misfits 3 text 600 text Posts: 1 Threads: 0 Joined: Jul 2024 Posts: 1,210 Threads: 146 Joined: Jul 2017 Just for fun I made this: from random import randint, choices, choice from string import ascii_lowercase, ascii_uppercase # doesn't matter how many compartments compartments = [(100, 200), (300, 400), (500, 600)] items_dict = {f'compartment{i + 1}': [] for i in range(len(compartments))} # fake some names def make_a_name(): first = choice(ascii_uppercase) second = ''.join(choices(ascii_lowercase, k=5)) name = first + second return name # run all lists of items together by adding # all_items = items1 + items2 + items3 # doesn't matter what the second element in each tuple of all_items is # here we only have tuples of this format (number, text) but the tuple can be (number, anything) all_items = [(randint(20, 700), make_a_name()) for i in range(randint(200, 400))] len(all_items) # like 350 or 236 # a generator in case there are a lot of compartments range_gen = (comp for comp in compartments) # check all items against all low and high # maybe the lists are not clearly separated # the list all_items may contain a number or a name more than once def check_num(key, low, high): # reset the generator items_gen each time around items_gen = (i for i in all_items) for item in items_gen: if low <= item[0] <= high: items_dict[key].append(item) # for each key in items_dict check all elements of all_items # the function check_num will assign the element to the correct compartment in items_dict for key in items_dict.keys(): low, high = next(range_gen) check_num(key, low, high) # sort the lists for key in items_dict.keys(): items_dict[key].sort() # print 10 elements from each result list for key in items_dict.keys(): for i in range(10): print(items_dict[key][i]) |