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.
List Comprehension (Recommended)
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()
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
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
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
Other Languages
Nested Lists
Common Pitfalls
- Modifying a list while iterating with
for x in lst:for x in numbers: x += ndoes not modify the list —xis a local variable. Use index-based iteration (numbers[i] += n) or create a new list with a comprehension. - Forgetting
list()aroundmap(): In Python 3,map()returns a lazy iterator, not a list.result = map(lambda x: x + n, numbers)gives a map object. Wrap it inlist()to materialize the results. - Using
+=on a shared reference:result = numbers; result = [x + n for x in result]creates a new list and rebindsresult, leavingnumbersunchanged. Butresult = numbers; for i in range(len(result)): result[i] += nmodifies 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]raisesTypeError. 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 + nfor large datasets (10-100x faster than pure Python) - Use
for i in range(len(lst)): lst[i] += nfor 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

