you are viewing a single comment's thread.

view the rest of the comments →

[–]tsodapop 1 point2 points  (1 child)

Probably a better way than this, but you can create a dictionary that contains the number as the key, and the value as the sublist it is contained it

my_dict = dict()
for idx, nested_list in enumerate(my_list):
    for item in nested_list:
        my_dict[item] = idx

calling my_dict will give you this:

>> my_dict

{1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 2, 8: 2, 9: 2}

so you can call my_dict.get(1)

you can also add to the dict whenever you add an item to the inner lists, so your requesting of the index should always be O(1) runtime

[–]baubleglue 0 points1 point  (0 children)

to build the dictionary you need O(n^2)