In this tutorial, you will learn an essential Python skill: how to perform a simple arithmetic operation like addition for two numbers within a list.

You will learn various ways to add two numbers in a list using the most common features in Python, including **for loops**, **list comprehensions**, and **lambda functions**. With this knowledge, you will be able to apply these methods to different problems and tasks in Python programming.

### Step 1: Create a List of Numbers

First, you need to create a list of numbers. The list can contain integers or floating-point numbers. For this tutorial, you will create a list of integers named **my_numbers**:

1 |
my_numbers = [1, 2, 3, 4, 5] |

### Step 2: Adding Two Numbers Using a For Loop

One of the simplest methods to add two numbers in a list is by using a **for loop**. In the following example, you will add each number in the list to the next number, then print the result:

1 2 3 |
for i in range(len(my_numbers) - 1): result = my_numbers[i] + my_numbers[i+1] print(result) |

#### Output

3 5 7 9

### Step 3: Adding Two Numbers Using List Comprehension

Another option for adding two numbers in a list is by using a **list comprehension**. List comprehensions are a concise way to create new lists by applying an expression on each element in an existing list. Here’s how you can implement the same addition operation as in step 2 using list comprehension:

1 2 |
result = [my_numbers[i] + my_numbers[i+1] for i in range(len(my_numbers) - 1)] print(result) |

#### Output

[3, 5, 7, 9]

### Step 4: Adding Two Numbers Using Lambda Functions and Map()

If you prefer a functional programming approach, you can use **lambda functions** and the **map()** function to add two numbers in a list. A lambda function is a small, anonymous function that can have any number of arguments, but can only have one expression.

1 2 |
result = list(map(lambda x, y: x + y, my_numbers[:-1], my_numbers[1:])) print(result) |

#### Output

[3, 5, 7, 9]

### Full Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
my_numbers = [1, 2, 3, 4, 5] # Using for loop for i in range(len(my_numbers) - 1): result = my_numbers[i] + my_numbers[i+1] print(result) # Using list comprehension result = [my_numbers[i] + my_numbers[i+1] for i in range(len(my_numbers) - 1)] print(result) # Using lambda functions and map() result = list(map(lambda x, y: x + y, my_numbers[:-1], my_numbers[1:])) print(result) |

### Conclusion

Now you have learned three different ways to add two numbers in a list using Python: with a for loop, list comprehension, and lambda functions combined with the map() function.

Understanding these methods will enable you to apply them to various Python problems and tasks. Moreover, this knowledge will also prove beneficial when working with more complex data structures in the future.