VOOZH about

URL: https://www.geeksforgeeks.org/python/python-adding-k-to-each-element-in-a-list-of-integers/

⇱ Python - Adding K to each element in a list of integers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Adding K to each element in a list of integers

Last Updated : 11 Jul, 2025

In Python, we often need to perform mathematical operations on each element. One such operation is adding a constant value, K, to every element in the list. In this article, we will explore several methods to add K to each element in a list.

Using List Comprehension

List comprehension provides a concise and readable way to add a constant value to each element in a list. It is the most efficient method in terms of speed and readability.


Output
[3, 4, 5, 6]

Explanation:

  • This method iterates over each element of the list and adds k to it.
  • The result is stored in 'b'. This approach is efficient and often preferred due to its simplicity and ease of use.

Let's see some more methods on how to add K to each element in a list of integers.

Using map() with lambda

The map()function applies a given function to all items in an input list. It can be used for adding a constant to each element in a list.


Output
[3, 4, 5, 6]

Explanation:

  • Here, map() applies a lambda function that adds k to each element in the list.
  • The result is converted to a list using list().

Using for Loop

A more traditional approach is using a for loop to iterate through the list and add k to each element.


Output
[3, 4, 5, 6]

Explanation:

  • In this approach, we manually append the modified elements to a new list.

Using NumPy

For more advanced use cases, especially with large lists or arrays, we can use NumPy to add a constant to each element in a list efficiently.


Output
[3 4 5 6]

Explanation:

  • With NumPy, we can add a constant to all elements of the list directly without the need for explicit iteration.
  • This is highly efficient for large datasets.
Comment