Return values of a list for specific list of indices from another list

Morse

I have a list with specific indices

list1 = [0,3,5]

and another list.

list2 = ['a','b','c','d','e','f']

I want a single line code to return items of list2 for indices mentioned in list1.

output = ['a','d','f']

I know its possible from comprehensive for loop with/without lambda function or something.

Attempts I was trying were something like below

[x for x in list2[y] if y in list1]
[m for m in list2 for f in list1]
[for x in i: list2[x]]

None of the attempts gave me expected results.

Please let me know how to achieve this!

Joe Iddon

Just use a list-comprehension:

[list2[i] for i in list1]
#['a', 'd', 'f']

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 a list of integers to reference the indices of another list to return a specific value from that list

From Dev

Finding the indices of the values of one list in another list

From Dev

Return a sorted list of indices from list of lists

From Dev

Is it possible to return a list of specific values from a query?

From Dev

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

From Dev

Substituting the values from list of lists by the values of another list based on indices in python

From Dev

Python creating specific list from another list

From Dev

Remove elements from a list, using another list as indices

From Dev

Changing value of variable in list using indices from another list

From Dev

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

From Dev

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

From Java

Sorting list based on values from another list?

From Dev

Sum values from a list, selected by another list

From Dev

Writing specific values from list

From Dev

Remove parts of a list based on another list of indices

From Dev

How do I return a list of the 3 lowest values in another list

From Dev

How do I return a list of the 3 lowest values in another list

From Dev

Swapping indices with values in a Python list?

From Dev

Python: matching values from one list to the sequence of values in another list

From Dev

Return a specific dict from a list of dicts

From Dev

Remove list of indices from a list in Python

From Dev

Extract list from given list and given indices

From Dev

Appending list B with indices from enumerated list A

From Dev

Return List value from another window

From Dev

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

From Dev

Creating a value list from dictionary with partial indices:values

From Dev

Remove items from a List which are in another List by a specific property?

From Dev

Remove items from a List which are in another List by a specific property?

From Dev

Sum of specific elements of a list based on indexes from another list in python

Related Related

  1. 1

    Using a list of integers to reference the indices of another list to return a specific value from that list

  2. 2

    Finding the indices of the values of one list in another list

  3. 3

    Return a sorted list of indices from list of lists

  4. 4

    Is it possible to return a list of specific values from a query?

  5. 5

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

  6. 6

    Substituting the values from list of lists by the values of another list based on indices in python

  7. 7

    Python creating specific list from another list

  8. 8

    Remove elements from a list, using another list as indices

  9. 9

    Changing value of variable in list using indices from another list

  10. 10

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

  11. 11

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

  12. 12

    Sorting list based on values from another list?

  13. 13

    Sum values from a list, selected by another list

  14. 14

    Writing specific values from list

  15. 15

    Remove parts of a list based on another list of indices

  16. 16

    How do I return a list of the 3 lowest values in another list

  17. 17

    How do I return a list of the 3 lowest values in another list

  18. 18

    Swapping indices with values in a Python list?

  19. 19

    Python: matching values from one list to the sequence of values in another list

  20. 20

    Return a specific dict from a list of dicts

  21. 21

    Remove list of indices from a list in Python

  22. 22

    Extract list from given list and given indices

  23. 23

    Appending list B with indices from enumerated list A

  24. 24

    Return List value from another window

  25. 25

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

  26. 26

    Creating a value list from dictionary with partial indices:values

  27. 27

    Remove items from a List which are in another List by a specific property?

  28. 28

    Remove items from a List which are in another List by a specific property?

  29. 29

    Sum of specific elements of a list based on indexes from another list in python

HotTag

Archive