Python programming
list manipulation
coding tutorial
beginner guide
programming basics

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The most Pythonic way to add an integer to each element in a list is a list comprehension: [x + n for x in lst]. For numerical computing, NumPy's vectorized addition (arr + n) is faster on large arrays. Other approaches include map() with a lambda, a for loop with in-place modification, and functional tools like operator.add with functools.partial. The choice depends on readability, performance requirements, and whether you need a new list or want to modify the existing one in place.

python
1numbers = [1, 2, 3, 4, 5]
2n = 10
3
4result = [x + n for x in numbers]
5print(result)  # [11, 12, 13, 14, 15]
6
7# Original list is unchanged
8print(numbers)  # [1, 2, 3, 4, 5]

List comprehensions are the standard Python approach — they are concise, readable, and faster than explicit for loops because they are optimized at the bytecode level.

Using map()

python
1numbers = [1, 2, 3, 4, 5]
2n = 10
3
4# With lambda
5result = list(map(lambda x: x + n, numbers))
6print(result)  # [11, 12, 13, 14, 15]
7
8# With operator.add and functools.partial
9from functools import partial
10from operator import add
11
12result = list(map(partial(add, n), numbers))
13print(result)  # [11, 12, 13, 14, 15]

map() returns a lazy iterator, so wrap it in list() to get a list. For simple operations, list comprehensions are preferred over map() with a lambda because they are more readable.

In-Place Modification with a for Loop

python
1numbers = [1, 2, 3, 4, 5]
2n = 10
3
4# Modify the existing list in place
5for i in range(len(numbers)):
6    numbers[i] += n
7
8print(numbers)  # [11, 12, 13, 14, 15]
python
1# Using enumerate (more Pythonic)
2numbers = [1, 2, 3, 4, 5]
3for i, val in enumerate(numbers):
4    numbers[i] = val + n

In-place modification avoids creating a new list, which saves memory for very large lists. However, it mutates the original data, which can cause bugs if other parts of the code reference the same list.

NumPy Vectorized Addition

python
1import numpy as np
2
3numbers = np.array([1, 2, 3, 4, 5])
4n = 10
5
6result = numbers + n
7print(result)  # [11 12 13 14 15]
8
9# Works with any arithmetic operation
10print(numbers * 2)      # [ 2  4  6  8 10]
11print(numbers ** 2)      # [ 1  4  9 16 25]
12print(numbers + np.array([10, 20, 30, 40, 50]))  # Element-wise addition

NumPy broadcasting applies the operation to every element without an explicit loop. For lists with more than a few thousand elements, NumPy is 10-100x faster than a list comprehension because the loop runs in optimized C code.

Performance Comparison

python
1import timeit
2
3numbers = list(range(100_000))
4n = 10
5
6# List comprehension
7timeit.timeit(lambda: [x + n for x in numbers], number=100)
8# ~0.45s
9
10# map + lambda
11timeit.timeit(lambda: list(map(lambda x: x + n, numbers)), number=100)
12# ~0.55s
13
14# for loop in-place
15def add_inplace():
16    nums = numbers.copy()
17    for i in range(len(nums)):
18        nums[i] += n
19timeit.timeit(add_inplace, number=100)
20# ~0.65s
21
22# NumPy
23import numpy as np
24arr = np.array(numbers)
25timeit.timeit(lambda: arr + n, number=100)
26# ~0.01s (45x faster)

Other Languages

javascript
1// JavaScript
2const numbers = [1, 2, 3, 4, 5];
3const result = numbers.map(x => x + 10);
4// [11, 12, 13, 14, 15]
java
1// Java (streams)
2int[] numbers = {1, 2, 3, 4, 5};
3int[] result = Arrays.stream(numbers).map(x -> x + 10).toArray();
4// [11, 12, 13, 14, 15]
ruby
1# Ruby
2numbers = [1, 2, 3, 4, 5]
3result = numbers.map { |x| x + 10 }
4# [11, 12, 13, 14, 15]

Nested Lists

python
1# Add to each element in a 2D list
2matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3n = 10
4
5result = [[x + n for x in row] for row in matrix]
6print(result)  # [[11, 12, 13], [14, 15, 16], [17, 18, 19]]
7
8# NumPy handles this naturally
9import numpy as np
10matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
11print(matrix + n)
12# [[11 12 13]
13#  [14 15 16]
14#  [17 18 19]]

Common Pitfalls

  • Modifying a list while iterating with for x in lst: for x in numbers: x += n does not modify the list — x is a local variable. Use index-based iteration (numbers[i] += n) or create a new list with a comprehension.
  • Forgetting list() around map(): In Python 3, map() returns a lazy iterator, not a list. result = map(lambda x: x + n, numbers) gives a map object. Wrap it in list() to materialize the results.
  • Using += on a shared reference: result = numbers; result = [x + n for x in result] creates a new list and rebinds result, leaving numbers unchanged. But result = numbers; for i in range(len(result)): result[i] += n modifies the shared list, affecting both variables.
  • Type errors with mixed types: ["1", "2", "3"] is a list of strings, not integers. [x + 10 for x in lst] raises TypeError. Convert first: [int(x) + 10 for x in lst].
  • Using NumPy for small lists: NumPy has overhead for array creation. For lists under 100 elements, a list comprehension is actually faster than converting to NumPy, adding, and converting back.

Summary

  • Use list comprehension [x + n for x in lst] for the standard Pythonic approach
  • Use NumPy arr + n for large datasets (10-100x faster than pure Python)
  • Use for i in range(len(lst)): lst[i] += n for in-place modification
  • map(lambda x: x + n, lst) works but is less readable than a list comprehension
  • For nested lists, use nested comprehensions or NumPy broadcasting
  • Always verify element types — string elements need conversion to int before addition

Course illustration
Course illustration

All Rights Reserved.