How to add number to list python

In this tutorial, we will learn different ways to add elements to a List in Python.

Methods to add elements to List in Python

There are four methods to add elements to a List in Python.

  1. append(): append the object to the end of the list.
  2. insert(): inserts the object before the given index.
  3. extend(): extends the list by appending elements from the iterable.
  4. List Concatenation: We can use + operator to concatenate multiple lists and create a new list.

Python add elements to List Examples

We can add an element to the end of the list or at any given index. There are ways to add elements from an iterable to the list. We can also use + operator to concatenate multiple lists to create a new list.

1. append()

This function add the element to the end of the list.

fruits = ["Apple", "Banana"]

# 1. append()
print(f'Current Fruits List {fruits}')

f = input("Please enter a fruit name:\n")
fruits.append(f)

print(f'Updated Fruits List {fruits}')

Output:

Current Fruits List ['Apple', 'Banana']
Please enter a fruit name:
Orange
Updated Fruits List ['Apple', 'Banana', 'Orange']

2. insert()

This function adds an element at the given index of the list. It’s useful to add an element at the specified index of the list.

num_list = [1, 2, 3, 4, 5]

print(f'Current Numbers List {num_list}')

num = int(input("Please enter a number to add to list:\n"))

index = int(input(f'Please enter the index between 0 and {len(num_list) - 1} to add the number:\n'))

num_list.insert(index, num)

print(f'Updated Numbers List {num_list}')

Output:

Current Numbers List [1, 2, 3, 4, 5]
Please enter a number to add to list:
20
Please enter the index between 0 and 4 to add the number:
2
Updated Numbers List [1, 2, 20, 3, 4, 5]

3. extend()

This function append iterable elements to the list. It is useful to append elements from an iterable to the end of the list.

list_num = []
list_num.extend([1, 2])  # extending list elements
print(list_num)
list_num.extend((3, 4))  # extending tuple elements
print(list_num)
list_num.extend("ABC")  # extending string elements
print(list_num)

Output:

[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, 'A', 'B', 'C']

4. List Concatenation

If you have to concatenate multiple lists, you can use the “+” operator. This will create a new list and the original lists will remain unchanged.

evens = [2, 4, 6]
odds = [1, 3, 5]

nums = odds + evens
print(nums)  # [1, 3, 5, 2, 4, 6]

The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

Conclusion

It’s very easy to add elements to a List in Python programming. We can append an element at the end of the list, insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator. References:

  • Python List
  • Python.org Docs

Use append() function to add numbers in a list Python. Syntax of adding a single number into a given list.

lst.append(numbers)

Simple example code.

Use list.append()

It will append new elements into a list.

a_list = [1, 2, 3]

new_num = 4

a_list.append(new_num)

print(a_list)

Output: [1, 2, 3, 4]

Use enumerate()

Use it if you have an iterable object to add-in list. It will add value to the existing list number.

a_list = [1, 0, 0]

new_nums = [1, 2, 3]

for index, integer in enumerate(new_nums):
    a_list[index] += integer

print(a_list)

Output: [2, 2, 3]

Add user input numbers into the list

lst = []
num = int(input('How many numbers: '))

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)
    
print("Sum of elements in given list is :", sum(lst))

Output:

How to add number to list python

Do comment if you have any doubts or suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

How to add number to list python

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

How do you add an integer to a list?

Use the list. append() method to add integers to a list in Python, e.g. my_list. append(6) . The list.

How do you add items to a list in Python?

Python List insert().
Syntax of List insert() The syntax of the insert() method is list.insert(i, elem) ... .
insert() Parameters. The insert() method takes two parameters: ... .
Return Value from insert() ... .
Example 1: Inserting an Element to the List. ... .
Example 2: Inserting a Tuple (as an Element) to the List..

How do I add two numbers in a list in Python?

Practical Data Science using Python.
Take two lists l1 and l2. Initialize head and temp as null..
c := 0..
while l1 and l2 both are non-empty lists. if l1 is non-empty, then set a := 0, otherwise set a := l1.val. ... .
if c is non-zero, then. node := new node with value 1, next of head := node..
return temp..

Can you += a list in Python?

Conclusion. So + and += for lists are still concatenation, just slightly different versions: + is list-to-list concatenation, and returns a new list. += concatenates the items of any sequence or iterator to the existing list, in-place.