How to insert numbers in python

In this tutorial, we will learn about the Python List insert() method with the help of examples.

The insert() method inserts an element to the list at the specified index.

Example

# create a list of vowels
vowel = ['a', 'e', 'i', 'u']

# 'o' is inserted at index 3 (4th position) vowel.insert(3, 'o')

print('List:', vowel) # Output: List: ['a', 'e', 'i', 'o', 'u']


Syntax of List insert()

The syntax of the insert() method is

list.insert(i, elem)

Here, elem is inserted to the list at the ith index. All the elements after elem are shifted to the right.


insert() Parameters

The insert() method takes two parameters:

  • index - the index where the element needs to be inserted
  • element - this is the element to be inserted in the list

Notes:

  • If index is 0, the element is inserted at the beginning of the list.
  • If index is 3, the index of the inserted element will be 3 (4th element in the list).


Return Value from insert()

The insert() method doesn't return anything; returns None. It only updates the current list.


Example 1: Inserting an Element to the List

# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]

# insert 11 at index 4 prime_numbers.insert(4, 11)

print('List:', prime_numbers)

Output

List: [2, 3, 5, 7, 11]

Example 2: Inserting a Tuple (as an Element) to the List

mixed_list = [{1, 2}, [5, 6, 7]]

# number tuple
number_tuple = (3, 4)

# inserting a tuple to the list mixed_list.insert(1, number_tuple)

print('Updated List:', mixed_list)

Output

Updated List: [{1, 2}, (3, 4), [5, 6, 7]]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while dealing with strings, we may encounter a problem in which we might have a numeric variable whose value keeps changing and we need to print the string including that number. Strings and numbers being different data types have to be solved in different ways. Let’s discuss certain ways in which this problem can be solved. 

    Method #1 : Using Type conversion The simplest way in which this task can be performed is by converting the integer explicitly into string datatype using the basic type conversion and adding it to appropriate position. 

    Python3

    test_str = "Geeks"

    test_int = 4

    print("The original string is : " + test_str)

    print("The original number : " + str(test_int))

    res = test_str + str(test_int) + test_str

    print("The string after adding number is  : " + str(res))

    Output : 

    The original string is : Geeks
    The original number : 4
    The string after adding number is  : Geeks4Geeks

    Method #2: Using %d operator This operator can be used to format the string to add the integer. The “d” represents that the datatype to be inserted to string is an integer. This can be changed according to the requirements. 

    Python3

    test_str = "Geeks"

    test_int = 4

    print("The original string is : " + test_str)

    print("The original number : " + str(test_int))

    res = (test_str + "% d" + test_str) % test_int

    print("The string after adding number is  : " + str(res))

    Output : 

    The original string is : Geeks
    The original number : 4
    The string after adding number is  : Geeks4Geeks

    Method #3: Using join() method

    Python3

    test_str = "Geeks"

    test_int = 4

    print("The original string is : " + test_str)

    print("The original number : " + str(test_int))

    res=[test_str]*2

    res=str(test_int).join(res)

    print("The string after adding number is : " + str(res))

    Output

    The original string is : Geeks
    The original number : 4
    The string after adding number is : Geeks4Geeks


    How do you insert a number in Python?

    How to Add Two Numbers in Python.
    ❮ Previous Next ❯.
    Example. x = 5. y = 10. print(x + y) Try it Yourself ».
    Example. x = input("Type a number: ") y = input("Type another number: ") sum = int(x) + int(y) print("The sum is: ", sum) Try it Yourself ».
    ❮ Previous Next ❯.

    What is insert () in Python?

    insert() Function is a Python library function that is used to insert the given element at a particular index in a list. Syntax of the insert() function is, My_list.insert(index, element) insert() function takes 2 parameters, index and element. There is no return value in insert() function in Python.

    How do you add numbers to a string in Python?

    Method #2: Using %d operator This operator can be used to format the string to add the integer. The “d” represents that the datatype to be inserted to string is an integer.

    How do you add all the numbers in a list in Python?

    Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.