You are currently viewing Add 1 to each Element in the List in Python

To add 1 to each element of the list in Python you can use the list comprehension, map with a lambda expression, zip function, for loop, and many more. In this article, we will discuss several methods to add1 to each element in the list in python.

Advertisements

Following are the methods to add 1 to each element of the list in Python.

  1. Using List Comprehension
  2. Using map() & lambda expression
  3. Using map() & operator.add() method
  4. Using zip() function
  5. Using itertools.zip_longest() function
  6. Using for loop

1. Quick Examples of adding 1 to each element in the List

Following are quick examples of adding value 1 to each element (all elements) in the list.


# Quick Examples of adding 1 to all elements in the List

# Consider list of marks
student_marks=[90,80,78,98,100]

# Value to add to the element
value=1

# Using List Comprehension
print([i + value for i in student_marks])

# Using map() + lambda expression
print(list(map(lambda i : i + value, student_marks)))

# Using map() + operator.add
import operator
print(list(map(operator.add, student_marks, [value] * len(student_marks))))

# Using zip()
print([i + j for i,j in zip(student_marks, [value] * len(student_marks))])

# Using zip_longest()
import itertools
print([i + j for i,j in itertools.zip_longest(student_marks, [value] * len(student_marks))])

# Using for loop
list2=[]
for i in student_marks:
  list2.append(i+value)
print(list2)

2. Using for loop

First, we will iterate each element in a list using for loop and add 1 to each element and then append the result to the new list using the list.append() method. Now, this new list holds elements with value 1 added to the original value.

2.1 Example

Create a list of 5 marks and add 1 to each integer using for loop with the append() method.


# Consider list of marks
student_marks=[90,80,78,98,100]

# Value to add to the element
value=1

# Using for loop to add 1
list2=[]
for i in student_marks:
  list2.append(i+value)

print("Actual Marks: ",student_marks)
print("Final Marks: ",list2)

# Output:
# Actual Marks:  [90, 80, 78, 98, 100]
# Final Marks:  [91, 81, 79, 99, 101]

3. Using List Comprehension

In this approach, we will iterate each element in a list using for loop inside list comprehension and return each value by adding 1 to it.

3.1 Syntax

Following is the syntax of using list comprehension.


# Syntax
[iterator + value for iterator in list1]

Where list1 is the input list.

3.2 Example

Create a list of 5 marks and add 1 to each integer using list comprehension.


# Consider list of marks
student_marks=[90,80,78,98,100]
print("Actual Marks: ",student_marks)

# Value to add to the element
value=1

# Using List Comprehension
print("Final Marks: ",[i + value for i in student_marks])

# Output:
# Actual Marks:  [90, 80, 78, 98, 100]
# Final Marks:  [91, 81, 79, 99, 101]

4. Using map() with lambda expression

The map() takes two parameters. The first parameter is the lambda expression which adds one to each value and the second parameter is the input list.

4.1 Syntax

Let’s see the syntax of using map() with a lambda expression.


# Syntax
list(map(lambda x : x + 1, list1))

4.2 Example

Create list of 5 marks and add 1 to each integer using map() and lambda expression. Here, each element is passed to the lambda and we have a lambda expression that adds 1 to each element.


# Consider list of marks
student_marks=[90,80,78,98,100]

# Value to add to the element
value=1

# Using map() + lambda expression
print("Actual Marks: ",student_marks)
print("Final Marks: ",list(map(lambda i : i + value, student_marks)))

# Output:
# Actual Marks:  [90, 80, 78, 98, 100]
# Final Marks:  [91, 81, 79, 99, 101]

5. Using map() with lambda expression

In this scenario, we will provide three parameters to the map() function. The first parameter is the operator.add method which will perform an addition operation. The second parameter is the input list and the last parameter is the list of length equal to the list1 that has 1’s.

5.1 Syntax

Following is the syntax of using map() with operator.add


# Where, list1 is the input list.
list(map(operator.add, list1 , [value] * len(list1 )))

5.2 Example

Create a list of 5 marks and add 1 to each integer using map() and operator.add


import operator
# Consider list of marks
student_marks=[90,80,78,98,100]
print("Actual Marks: ",student_marks)

value=1
# Using map() + operator.add
print("Final Marks: ",list(map(operator.add, student_marks, [value] * len(student_marks))))

# Output:
# Actual Marks:  [90, 80, 78, 98, 100]
# Final Marks:  [91, 81, 79, 99, 101]

6. Using zip()

zip() in python is used to join two or more iterables.

In this scenario, we will create a list containing 1’s in which its length is equal to the length of the input list. Next, we will pass these two lists to the zip() function and iterate the zipped lists using the for loop inside the List Comprehension. It will return the values by adding two iterables.

6.1 Syntax

Let’s see the syntax of using zip() function.


# Where, list1 is the input list.
[iterator1 + iterator2 for iterator1 ,iterator2 in zip(list1, [1] * len(list1))]

6.2 Example

The following example uses the zip() function to add 1 to each element of the Python list.


# Consider list of marks
student_marks=[90,80,78,98,100]
print("Actual Marks: ",student_marks)

value=1
# Using zip()
print("Final Marks: ",[i + j for i,j in zip(student_marks, [value] * len(student_marks))])

# Output:
# Actual Marks:  [90, 80, 78, 98, 100]
# Final Marks:  [91, 81, 79, 99, 101]

7. Using zip_longest()

The zip_longest() in python is available in itertools module which is used to join two or more iterables.

In this scenario, we will create a list containing 1’s in which its length is equal to the length of the input list. Next, we will pass these two lists to the zip_longest() function and iterate the zipped lists using the for loop inside the List Comprehension. It will return the values by adding two iterables.

7.1 Syntax

Let’s see the syntax of using zip_longest() function.


# Where, list1 is the input list.
[iterator1 + iterator2 for iterator1 ,iterator2 in itertools.zip_longest(list1, [1] * len(list1))]

7.2 Example


# Import itertools
import itertools

# Consider list of marks
student_marks=[90,80,78,98,100]
print("Actual Marks: ",student_marks)

value=1
# Using itertools.zip_longest()
print("Final Marks: ",[i + j for i,j in itertools.zip_longest(student_marks, [value] * len(student_marks))])

# Output:
# Actual Marks:  [90, 80, 78, 98, 100]
# Final Marks:  [91, 81, 79, 99, 101]

8. Conclusion

We have seen how to add 1 to each element in the list using several approaches. We can add any value by changing value variable. zip() and zip_longest() methods are similar in this approach and for loop and list comprehension are also same. We used map() function by passing lambda expression and operator.add method.