Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to add to numbers in list in python

I want to be able to add a number to a number in list. I do not want to add numbers TO a list. I want to add to numbers IN a list. There is just 2 numbers in the list and I want to add to each of those. I want to add the same number to each, 1.

When I tried making a list and just saying list + 1 I got a Type error. Here is what I tried:

my_list = [2 , 3 ]
my_list1 = my_list + 1
print(my_list)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

my_list = [2,3]

#Solution 1
my_list = [item +1 for item in my_list]

#Solution 2 
for i in range(len(my_list)):
   my_list[i] += 1
#Solution 3 
my_list = list(map(lambda x: x + 1, my_list))

#Solution 4
import numpy as np
my_list = np.array(my_list) + 1
my_list = my_list.tolist()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading