[Python] Using .pop() with "x for x in", and indexing multiple indexes in an array

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    was wondering if it's possible & how you'd go about using x for x in with a function like list.pop?

    It's easily achievable for me with the normal for-loop but it always returns the generator object when doing aList.pop(x for x in [0,1,2]).

    I'm sure I'm missing something obvious, but the way I've achieved it otherwise is just

    for x in [0,1,2]:
    aList.pop(x)


    And another question as well, what is the simplest way to choose 2 non-sequential indexes from an array without? In other words, what would the syntax be for something like y = aList[1,3] with the expected output being y assigned an array of the 1st and 3rd index of aList?

    I assume there's some trick rather than appending it, maybe not but was just wondering if there was a neat way of selecting multiple indexes from a list in 1 go.

    Thanks.

    aList.pop(x for x in [0,1,2]), expected to remove indexes 1, 3 & 5 but instead returns error, as (x for x in [0,1,2]) returns generator object.

    Also, was looking for the best way to assign list y = aList[1,3], assigning y as a list with the values from aList's 1st and 3rd index.

    Continue reading...

Compartilhe esta Página