[Python] How to merge 2 lists and sort them based on index?

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

  1. Stack

    Stack Membro Participativo

    I have the following dataframe:

    idx​
    val1​
    val2​
    0​
    15​
    12​
    1​
    14​
    38​
    2​
    11​
    88​
    3​
    95​
    21​
    4​
    19​
    98​
    5​
    12​
    48​
    6​
    35​
    38​
    7​
    25​
    39​
    8​
    65​
    28​

    I created two lists based on index say.

    list1 = [0, 3, 6]
    list2 = [5, 8]


    I tried to write a code wherein index values from list1 takes val1 data and list2 takes val2 data and same is sorted on index.

    My output list should be

    output = [15, 95, 48, 35, 28]

    Continue reading...

Compartilhe esta Página