0

Let's say I have the following main_list that contains other small_lists:

main_list = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

I want to find the small_list knowing the index 0 and 1 of the small_lists. So for instance, how can I create a getlist() function such that:

getlist(4,5) = [4, 5, 6, 7]
getlist(0,1) = [0, 1, 2, 3]
getlist(5,5) = None
getlist(9,10) = None
2
  • I didn't get you. Commented Jun 15, 2018 at 4:40
  • 1
    This is too broad. How do you handle negative indices? How do you handle slices? Why do you specify the start in terms of the inner lists but the end - start in terms of the outer list? Commented Jun 15, 2018 at 4:40

4 Answers 4

2

The simplest way is to map your list to a dictionary, where the keys are set to what you wan't to retrieve by. This can be done quickly using list comprehension as per the example below.

>>> main_list = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

>>> indexed_list = { (i[0], i[1]) : i for i in main_list }

>>> indexed_list.get( (0,1) )
[0, 1, 2, 3]

>>> indexed_list.get( (4,5) )
[4, 5, 6, 7]

>>> indexed_list.get( (5,5) )
None
Sign up to request clarification or add additional context in comments.

2 Comments

That's great. Thanks! Didn't realize that dictionary keys could be defined as tuples.
Can be defined as anything, the true underlying key is the object hash of whatever entity your using.
0

The code below should solve your problem, or at least get you on the right track. It is also available here on OnlineGDB for further tinkering, if you need.

main_list = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

def getlist(idx0, idx1):
    # Assumes there isn't a match, unless one is found
    target_list = None

    # Loops through the list of lists, checking for a match with the characters at index 0 and 1
    for sub_list in main_list:
        if idx0 == sub_list[0] and idx1 == sub_list[1]:
            target_list = sub_list

    # returns the target list (if it found) or None (if it isn't)
    return target_list

print(getlist(4,5))
>>[4, 5, 6, 7]
print(getlist(0,1))
>>[0, 1, 2, 3]
print(getlist(5,5))
>>None
print(getlist(9,10))
>>None

Comments

0

Checking sliced list would work here, This also works if there are more than one matching list with starting indexes similar to argument.

>>> main_list = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
>>> def get_list(tup, list_group):
...     return [list_ for list_ in list_group if list_[:2] == list(tup)]
...
>>> print(get_list((0, 1), main_list))
[[0, 1, 2, 3]]
>>> main_list = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [0, 1, 10, 7]]  # multiple occurence
>>> print(get_list((0, 1), main_list))
[[0, 1, 2, 3], [0, 1, 10, 7]]

Comments

0

That's a weird way of classifying your elements. There are more suitable way's of storing your data lice dictionaries.

main_list = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
def getlist(a,b):
    for sub_list in main_list:
        if [a,b] == sub_list[0:2]:
            return sub_list

>>> print (getlist(4,5))
>>> print (getlist(2,1))
[4, 5, 6, 7]
None

1 Comment

Theres a typo in 4th line, it should be sub_list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.