How to add an integer to each element in a list?

user1212818 picture user1212818 · Feb 16, 2012 · Viewed 240.1k times · Source

If I have list=[1,2,3] and I want to add 1 to each element to get the output [2,3,4], how would I do that?

I assume I would use a for loop but not sure exactly how.

Answer

Ned Batchelder picture Ned Batchelder · Feb 16, 2012
new_list = [x+1 for x in my_list]