Adding a number to a list is a basic operation in Python. In this tutorial, you will learn how to add a number to a list using different methods: **append()**, **insert()**, and **list comprehension**.

Each method has its own advantages and is suitable for different situations. By the end of this tutorial, you should be able to choose the appropriate method for your specific needs and efficiently add numbers to a list in Python.

### Method 1: Using the append() function

The `append()`

function is the simplest and most common way of adding an element to a list in Python. It adds the specified element at the end of the list. Below is an example of how to use the `append()`

function:

1 2 3 4 |
numbers = [1, 2, 3, 4, 5] number_to_add = 6 numbers.append(number_to_add) |

[1, 2, 3, 4, 5, 6]

### Method 2: Using the insert() function

The `insert()`

function allows you to add an element to your list at a specific index. The syntax for this function is `list.insert(index, element)`

. Here’s an example of how to use `insert()`

:

1 2 3 4 5 |
numbers = [1, 2, 4, 5] number_to_add = 3 index = 2 numbers.insert(index, number_to_add) |

[1, 2, 3, 4, 5]

In this example, the `number_to_add`

(3) is added to the `numbers`

list at the specified `index`

(2).

### Method 3: Using list comprehension

**List comprehension** is an advanced technique in Python that allows you to create new lists using existing ones in a concise and efficient manner. This method is especially useful when adding an element based on a condition or modifying the elements of the list along with adding a number to the list. Here’s an example of how to use list comprehension:

1 2 3 4 |
numbers = [1, 2, 3, 4, 5] number_to_add = 6 new_numbers = [x for x in numbers] + [number_to_add] |

[1, 2, 3, 4, 5, 6]

In this example, we use list comprehension to create a new list, `new_numbers`

, composed of the elements of the `numbers`

list and the `number_to_add`

.

## Full code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Method 1: Using the append() function numbers = [1, 2, 3, 4, 5] number_to_add = 6 numbers.append(number_to_add) print(numbers) # Method 2: Using the insert() function numbers = [1, 2, 4, 5] number_to_add = 3 index = 2 numbers.insert(index, number_to_add) print(numbers) # Method 3: Using list comprehension numbers = [1, 2, 3, 4, 5] number_to_add = 6 new_numbers = [x for x in numbers] + [number_to_add] print(new_numbers) |

## Output:

[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6]

## Conclusion

By following this tutorial, you have learned how to add a number to a list in Python using three different methods: **append()**, **insert()**, and **list comprehension**. Each method offers different advantages, making them suitable for different situations. Choose the method that best fits your needs and start adding numbers to your Python lists with ease!