Subset items in list of list based on indices

Jack Arnestad

I have a list like the following:

test = [['hi', 'hi2'], ['bye', 'bye2', 'by3']]

and would like to subset it based on another list of lists with the desired indices in it:

indices = [[0], [0, 2]] 

For a normal list of values and list of indices, it is easy to do:

[1d_list[i] for i in 1d_indices]

So I tried something similar for this list of lists, but it is not working

[[[t[i] for i in ii] for t in test] for ii in indices]

Could you please advise what is wrong?

The desired output for this part is

test_out = [['hi'], ['bye', 'by3']]

Also, how can I select the exact inverse of what is in indices?

For example, desired output would be:

test_out_2 = [['hi2'], ['bye2']]

Thanks! Jack

Ajax1234

You can use zip with enumerate:

test = [['hi', 'hi2'], ['bye', 'bye2', 'by3']]
indices = [[0], [0, 2]] 
new_test = [[a for i, a in enumerate(c) if i in b] for c, b in zip(test, indices)]

Output:

[['hi'], ['bye', 'by3']]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find the indices of a subset in a list?

From Dev

Finding the Indices of multiple items in a list, using a list

From Dev

Subset list based on a condition in R

From Dev

subset files in a folder based on a list

From Dev

Cumulative addition in a list based on an indices list

From Dev

Remove parts of a list based on another list of indices

From Dev

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

From Dev

Transforming list to dict with list items as keys and list indices as values in python

From Dev

How to move a subset of items in a list in c#?

From Dev

How do I subset a list in R by every 3 indices?

From Dev

Finding indices of items from a list in another list even if they repeat

From Dev

Changing values of the dataframe columns based on a list of indices

From Dev

Get element from array based on list of indices

From Dev

TensorFlow: Create a new tensor based on list of indices

From Dev

Access the element of a list with map based on permutations of the indices

From Dev

Subset a data frame based on elements in a list

From Dev

Return dataframe subset based on a list of boolean values

From Dev

Subset a data frame based on elements in a list

From Dev

Grouping items in a list based on their contents

From Dev

Replace items in column based on list

From Dev

Grouping items in a list based on their contents

From Dev

Merge list items based on condition within the list

From Dev

Merge list items based on condition within the list

From Java

How to split a dataframe in a list based on another list that contains observations indices?

From Dev

Summing up elements from one list based on indices in another list

From Dev

Remove items from a list in Python based on previous items in the same list

From Java

Get indices of items in numpy array, where values is in list

From Dev

Adding items to a list based on a reference to parent item

From Java

Slice a list based on an index and items behind it in Python

Related Related

  1. 1

    How to find the indices of a subset in a list?

  2. 2

    Finding the Indices of multiple items in a list, using a list

  3. 3

    Subset list based on a condition in R

  4. 4

    subset files in a folder based on a list

  5. 5

    Cumulative addition in a list based on an indices list

  6. 6

    Remove parts of a list based on another list of indices

  7. 7

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

  8. 8

    Transforming list to dict with list items as keys and list indices as values in python

  9. 9

    How to move a subset of items in a list in c#?

  10. 10

    How do I subset a list in R by every 3 indices?

  11. 11

    Finding indices of items from a list in another list even if they repeat

  12. 12

    Changing values of the dataframe columns based on a list of indices

  13. 13

    Get element from array based on list of indices

  14. 14

    TensorFlow: Create a new tensor based on list of indices

  15. 15

    Access the element of a list with map based on permutations of the indices

  16. 16

    Subset a data frame based on elements in a list

  17. 17

    Return dataframe subset based on a list of boolean values

  18. 18

    Subset a data frame based on elements in a list

  19. 19

    Grouping items in a list based on their contents

  20. 20

    Replace items in column based on list

  21. 21

    Grouping items in a list based on their contents

  22. 22

    Merge list items based on condition within the list

  23. 23

    Merge list items based on condition within the list

  24. 24

    How to split a dataframe in a list based on another list that contains observations indices?

  25. 25

    Summing up elements from one list based on indices in another list

  26. 26

    Remove items from a list in Python based on previous items in the same list

  27. 27

    Get indices of items in numpy array, where values is in list

  28. 28

    Adding items to a list based on a reference to parent item

  29. 29

    Slice a list based on an index and items behind it in Python

HotTag

Archive