[Python] Explicitly select items from a list or tuple

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

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

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


    I can say

    >>> myList[0:3]
    ['foo', 'bar', 'baz']
    >>> myList[::2]
    ['foo', 'baz']
    >>> myList[1::2]
    ['bar', 'quux']


    How do I explicitly pick out items whose indices have no specific patterns? For example, I want to select [0,2,3]. Or from a very big list of 1000 items, I want to select [87, 342, 217, 998, 500]. Is there some Python syntax that does that? Something that looks like:

    >>> myBigList[87, 342, 217, 998, 500]

    Continue reading...

Compartilhe esta Página