Explicitly select items from a list or tuple

前端 未结 8 1946

I have the following Python list (can also be a tuple):

myList = [\'foo\', \'bar\', \'baz\', \'quux\']

I can say

>>&g         


        
8条回答
  •  误落风尘
    2020-11-27 11:05

    Maybe a list comprehension is in order:

    L = ['a', 'b', 'c', 'd', 'e', 'f']
    print [ L[index] for index in [1,3,5] ]
    

    Produces:

    ['b', 'd', 'f']
    

    Is that what you are looking for?

提交回复
热议问题