0

I know we get to return only one time but is it possible to increment all elements of a list at once.

I know I can do this

def test(lst, increment):
   newlst = []
   for value in lst:
      newlst.append(value + increment)
   return newlst
print(test([1,2],1))

OutPut = [2, 3]

I feel like the above code is little more work Is it possible to return all incremented values?

def test(lst, increment):
    for value in lst:
       return value + increment

print(test([1,2],1))

OutPut = 2, 3
RoyM
  • 735
  • 3
  • 14
TulMagar
  • 49
  • 1
  • 8

1 Answers1

2

You can use list-comprehension:

def incr(lst, i):
    return [x+i for x in lst]
RoyM
  • 735
  • 3
  • 14
  • I knew this idea but thank you. this idea works with multiple lists and subtracts value to each other. Also, we can join str with num. – TulMagar Oct 05 '19 at 01:39