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!
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.
Comments