howtouselinux

Understanding Python List Insert method

Table of Contents

 

The Python list insert() is an inbuilt method that inserts an element to the list at the specified index.  All the elements after this element will be shifted to the right. The Python list insert() method doesn’t return anything. It only updates the current list.

Understanding Python list insert Method

  • All the elements after element are shifted to the right.
  • The list is modified in place.
  • The insert() method is list method and can be called only on list values, not on other values such as strings or integers.
  • If the index is too high and out of range, the method places the new value at the end of the list. And it inserts the new value at the beginning of the list if the index is too low.

 

Python list insert() Syntax

The syntax of the insert() method in Python is list.insert(index, element). Element is inserted to the list at the ith index. All the elements after elem are shifted to the right.The insert() method doesn’t return anything. It returns None. It only updates the current list.

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

 

Python list insert: add item at specific index

An index is an integer value that refers to a specific position or element in a list. A negative index is an index that is numbered from the back of a list, instead of from the front.The first item in a list has an index of 0, while the last item in a list has an index of -1.

With list [1, 2, 3, 4],

  • index 0 refers to the first item in the list and is therefore 1.
  • index 1 refers to the second item in the list and is therefore 2.
  • index -1 refers to the last item in the list and is therefore 4.
  • index -2 refers to the second last item in the list and is therefore 3.

 

In this example, the code inserts the value ‘Google’ to the list.The insert() method opens a space at position 1 and stores the value ‘Google’ at that location. This operation shifts every other value in the list one position to the right:

clouds = [‘Cisco’, ‘AWS’, ‘IBM’]
clouds.insert(1, ‘Google’)
print(clouds)
[‘Cisco’, ‘Google’, ‘AWS’, ‘IBM’]

add item to the beginning of list – at index 0

clouds = [‘Cisco’, ‘AWS’, ‘IBM’]
clouds.insert(0, ‘Google’)
print(clouds)
[‘Google’, ‘Cisco’, ‘AWS’, ‘IBM’]

add item to the end of list

a.insert(len(a),x) is equivalent to a.append(x)
clouds = [‘Cisco’, ‘AWS’, ‘IBM’]
clouds.insert(len(clouds), ‘Google’)
print(clouds)
[‘Google’, ‘Cisco’, ‘AWS’, ‘IBM’]

Python list insert: add item using index which is out of range

A negative index is an index that is numbered from the back of a list, instead of from the front.For example, the index -1 refers to the last item in a list, while the index -2 refers to the second-last item in a list.The insert() method can be used to add an item in a list using a negative index.

If the index is out of range, the method places the new value at the end of the list if the index is too high to be in range, and it inserts the new value at the beginning of the list if the index is too low.

number_list = [10, 20, 40] # Missing 30.
number_list.insert(2, 30 ) # At index 2 (third), insert 30.
print(number_list) # Prints [10, 20, 30, 40]
number_list.insert(100, 33)
print(number_list) # Prints [10, 20, 30, 40, 33]
number_list.insert(-100, 44)
print(number_list) # Prints [44, 10, 20, 30, 40, 33]

 

Python list insert: add list into another list

The insert() method can be used to insert another list into a list.

mixed_list = [{1, 2}, [5, 6, 7]]
number_list = [3, 4]
# inserting a list to the list
mixed_list.insert(1, number_list)
print(‘Updated List:’, mixed_list)
Updated List: [{1, 2}, [3, 4], [5, 6, 7]]

difference between python list insert and python list extend

The difference between list insert and list extend is that list insert inserts an element at a specified position in a list, while list extend inserts multiple elements into a list. With list insert, you specify the index of the position where you want to insert an element, and with list extend, the elements are inserted at the end of the list.

Here is an example of using list insert:
list = [‘apple’, ‘banana’]
list.insert(1, ‘cherry’)

This would result in the list being: [‘apple’, ‘cherry’, ‘banana’]

And here is an example of using list extend:
list = [‘apple’, ‘banana’]
list.extend([‘cherry’, ‘fig’])

This would result in the list being: [‘apple’, ‘banana’, ‘cherry’, ‘fig’]

difference between python list insert and python list append

The difference between list insert and list append is that list insert inserts an element at a specified position in a list, while list append inserts one element at the end of the list. With list insert, you specify the index of the position where you want to insert an element, and with list append, the elements are inserted at the end of the list.

Here is an example of using list insert:
list = [‘apple’, ‘banana’]
list.insert(1, ‘cherry’)

This would result in the list being: [‘apple’, ‘cherry’, ‘banana’]

And here is an example of using list append:
list = [‘apple’, ‘banana’]
list.append([‘cherry’, ‘fig’])

This would result in the list being: [‘apple’, ‘banana’, [‘cherry’, ‘fig’]]

How to get the index of an element in a list?

The index() method can be used to find the index of an element in a list.For example, if we wanted to find the index of the element ‘banana’ in the list [‘apple’, ‘banana’], we could use the index() method like this:

list = [‘apple’, ‘banana’]
list.index(‘banana’)

This would result in the index being 1.

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press