find lists that start with items from another list

Alex Kamphuis

I have a manifold of lists containing integers. I store them in a list (a list of lists) that I call biglist.

Then I have a second list, eg [1, 2].

Now I want to find all lists out of the big_list that start with the same items as the small list. The lists I want to find must have at least all the items from the second list.

I was thinking this could be done recursively, and came up with this working example:

def find_lists_starting_with(start, biglist, depth=0):
    if not biglist:  # biglist is empty
        return biglist

    try:
        new_big_list = []
        # try:
        for smallist in biglist:
            if smallist[depth] == start[depth]:
                if not len(start) > len(smallist):
                    new_big_list.append(smallist)

        new_big_list = find_lists_starting_with(start,
                                                new_big_list,
                                                depth=depth+1)
        return new_big_list
    except IndexError:
        return biglist

biglist = [[1,2,3], [2,3,4], [1,3,5], [1, 2], [1]]
start = [1, 2]

print(find_lists_starting_with(start, biglist))

However I am not very satisfied with the code example.

Do you have suggestions as how to improve: - understandability of the code - efficiency

mortymacs

You can try it via an iterator, like so:

[x for x in big_list if x[:len(start_list)] == start_list]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using jQuery to find list items containing specific text in nested lists

From Dev

Find out if string list items startswith another item from another list

From Dev

Pythonic way to find 2 items from two lists existing in a another list

From Dev

Subtraction of one list of lists from another list of lists

From Dev

Create a list with items from another list at indices specified in a third list

From Dev

Find common items in list of lists of strings

From Dev

Adding items from pairs in list of lists

From Dev

How to filter one list of items from another list of items?

From Dev

Find items from List based on matching property

From Dev

Remove items from list if not shared between lists

From Dev

Excluding items from a list that exist in another list

From Dev

Java 8 streams: find items from one list that match conditions calculated based on values from another list

From Dev

Find duplicate items in all lists of a list of lists and remove them

From Dev

Compare items from lists and find similarity

From Dev

Find out if string list items startswith another item from another list

From Dev

How can i select random items from a set of two lists and then remove that set of items from another list built out of all the possibilities

From Dev

How to quickly convert from items in a list of lists to list of dictionaries in python?

From Dev

Find similar items in list of lists using Python

From Dev

Pythonic way to find 2 items from two lists existing in a another list

From Dev

Subtraction of one list of lists from another list of lists

From Dev

Find common items in list of lists of strings

From Dev

Remove list of items from another file in bash

From Dev

List of unique items from two lists with duplicates between each other

From Dev

Remove items from list if not shared between lists

From Dev

make new python lists with indexes from items in a single list

From Dev

Delete similar items that start the same from a list

From Dev

How to create list of items from another list

From Dev

Python List Comprehension: Affix strings from one list to the start of strings in another, for a list of lists

From Dev

Find items from the list based on given date

Related Related

  1. 1

    Using jQuery to find list items containing specific text in nested lists

  2. 2

    Find out if string list items startswith another item from another list

  3. 3

    Pythonic way to find 2 items from two lists existing in a another list

  4. 4

    Subtraction of one list of lists from another list of lists

  5. 5

    Create a list with items from another list at indices specified in a third list

  6. 6

    Find common items in list of lists of strings

  7. 7

    Adding items from pairs in list of lists

  8. 8

    How to filter one list of items from another list of items?

  9. 9

    Find items from List based on matching property

  10. 10

    Remove items from list if not shared between lists

  11. 11

    Excluding items from a list that exist in another list

  12. 12

    Java 8 streams: find items from one list that match conditions calculated based on values from another list

  13. 13

    Find duplicate items in all lists of a list of lists and remove them

  14. 14

    Compare items from lists and find similarity

  15. 15

    Find out if string list items startswith another item from another list

  16. 16

    How can i select random items from a set of two lists and then remove that set of items from another list built out of all the possibilities

  17. 17

    How to quickly convert from items in a list of lists to list of dictionaries in python?

  18. 18

    Find similar items in list of lists using Python

  19. 19

    Pythonic way to find 2 items from two lists existing in a another list

  20. 20

    Subtraction of one list of lists from another list of lists

  21. 21

    Find common items in list of lists of strings

  22. 22

    Remove list of items from another file in bash

  23. 23

    List of unique items from two lists with duplicates between each other

  24. 24

    Remove items from list if not shared between lists

  25. 25

    make new python lists with indexes from items in a single list

  26. 26

    Delete similar items that start the same from a list

  27. 27

    How to create list of items from another list

  28. 28

    Python List Comprehension: Affix strings from one list to the start of strings in another, for a list of lists

  29. 29

    Find items from the list based on given date

HotTag

Archive