How to delete the very last character from every string in a list of strings

后端 未结 4 614
长发绾君心
长发绾君心 2021-01-11 12:55

I have the strings \'80010\', \'80030\', \'80050\' in a list, as in

test = [\'80010\',\'80030\',\'80050\']

How can I delete the very last

4条回答
  •  难免孤独
    2021-01-11 13:18

    test = ["80010","80030","80050"]
    newtest = [x[:-1] for x in test]
    

    New test will contain the result ["8001","8003","8005"].

    [x[:-1] for x in test] creates a new list (using list comprehension) by looping over each item in test and putting a modified version into newtest. The x[:-1] means to take everything in the string value x up to but not including the last element.

提交回复
热议问题