[Python] Python: replacing multiple list items by one

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

  1. Stack

    Stack Membro Participativo

    I want to write a calculator program. My goal is to replace multiple list items(with datatype str) by one(int). I've tried the .insert() method, but it creates a new inner list and places it at the end of the main list.

    Something like this:

    input_list = ['4','5','6','+','8','7','4']

    #expected result
    output_list = [456, '+', 874]

    #actual result
    input_list = ['4','5','6','+','8','7','4',['4','5','6']]


    I also tried extend method and also without success.

    My code:

    num = ""
    start = ""
    for x in range(len(list)):
    if list[x].isdigit() == True:
    if start == "":
    start = x
    num += list[x]
    continue
    else:
    num += list[x]
    continue
    else:
    num = int(num)
    list.insert(num,list[start:x])
    num = ""
    start = ""
    continue

    Continue reading...

Compartilhe esta Página