Search code examples
pythonpython-3.xstringnumbers

How to add multiple numbers in one list (python)


Say if I want to add 11 to all the numbers in the variable "e". How would I do it?

ans = input("Enter here string of numbers here") 
add = input("How much do you want to add each individual numbers by: ")  #here is when it will minus each individual number print(output)

e = "21,45,42,71"

So it should now say (after you add 11 and print it)

32,56,53,82

code:

ans = input("Enter string of numbers here")
add = input("How much do you want to add each individual numbers by: ")

#here is when it will minus each individual number
print(output)```

Solution

  • try this:

    a= "21,45,42,71" #your string
    lst = list(map(int,a.split(","))) #splitted every number and converted to int and stored in list
    result = list(map(lambda x:x+11,lst)) #added 11 to each number
    print(result)