Which command is used to add 5 to the third position in list named LST?

Python List insert()

The Python List insert() method is an inbuilt function in Python that inserts a given element at a given index in a list.

Syntax:

list_name.insert(index, element)

Parameters:

  • index: the index at which the element has to be inserted.
  • element: the element to be inserted in the list.

Returns:



This method does not return any value but it inserts the given element at the given index.

Error:

If anything other than a list is used with insert(), then it returns an AttributeError.

Note:

If given index >= length(list) is given, then it inserts at the end of the list.

Example1: Inserting an Element to the List




# Python3 program for use
# of insert() method
list1 = [ 1, 2, 3, 4, 5, 6, 7 ]
# insert 10 at 4th index
list1.insert(4, 10)
print(list1)
list2 = ['a', 'b', 'c', 'd', 'e']
# insert z at the front of the list
list2.insert(0, 'z')
print(list2)

Output:

[1, 2, 3, 4, 10, 5, 6, 7] ['z', 'a', 'b', 'c', 'd', 'e']

Example 2: Error of insert() Method




# Python3 program for error
# of insert() method
# attribute error
string = "1234567"
string.insert(10, 1)
print(string)

Output:

Traceback (most recent call last): File "/home/2fe54bd8723cd0ae89a17325da8b2eb5.py", line 7, in string.insert(10, 1) AttributeError: 'str' object has no attribute 'insert'

Example 3: Insertion in a List Before any Element




# Python3 program for Insertion in a list
# before any element using insert() method
list1 = [ 1, 2, 3, 4, 5, 6 ]
# Element to be inserted
element = 13
# Element to be inserted before 3
beforeElement = 3
# Find index
index = list1.index(beforeElement)
# Insert element at beforeElement
list1.insert(index, element)
print(list1)

Output:

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

Example 4: Inserting a Tuple to the List




list1 = [ 1, 2, 3, 4, 5, 6 ]
# tuple of numbers
num_tuple = (4, 5, 6)
# inserting a tuple to the list
list1.insert(2, num_tuple)
print(list1)

Output:

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

Which command is used to add 5 to the third position in list named LST?




Article Tags :
Python
Python-Built-in-functions
python-list
python-list-functions
Practice Tags :
python-list

Python List insert() Negative Index

You can use a negative index in the lst.insert(index, element) method. With a negative index you count backwards, starting from the right. In other words, the index -1 stands for the rightmost element in the list. The insert() method inserts the element right in front of the index position. Thus, you get the following behavior where the element is inserted to the second last position:

>>> lst = ["Ann", "Bob", "Alice"] >>> lst.insert(-1, "Liz") >>> lst ['Ann', 'Bob', 'Liz', 'Alice']

What happens if you count further, i.e., -2, -3, or even -99? Let’s check:

>>> lst.insert(-2, "Sam") >>> lst ['Ann', 'Bob', 'Sam', 'Liz', 'Alice']

… and …

>>> lst.insert(-3, "Bob") >>> lst ['Ann', 'Bob', 'Bob', 'Sam', 'Liz', 'Alice']

… and …

>>> lst.insert(-99, "Hans") >>> lst ['Hans', 'Ann', 'Bob', 'Bob', 'Sam', 'Liz', 'Alice']

So you can see that every integer index is allowed! If you overshoot (e.g. -99), it will simply insert at the beginning of the list.

Python List insert() At Beginning

The insert() method allows you to add an element at the beginning of a list. Simply use the index 0 in the call lst.insert(0, element) to add element to the beginning of the lst.

Here’s an example:

>>> lst = [1, 2, 3] >>> lst.insert(0, 99) >>> lst [99, 1, 2, 3]

The insert(i, x) method inserts an element x at position i in the list. This way, you can insert an element to each position in the list—even at the first position. Note that if you insert an element at the first position, each subsequent element will be moved by one position. In other words, element i will move to position i+1.

Python List insert() At End

Okay, you can insert an element at every position in the list—just use the first argument to define the insertion index. Consequently, you can “insert” an element at the end of a list by defining an index that’s greater or equal the size of the list. Remember, insert(index, element) inserts element before the element that’s currently at position index. So if you define an index that’s at least the size of the list, you’ll insert the element at the end of the list:

>>> lst = [1, 2, 3, 4, 5, 6] >>> lst.insert(6, 99) >>> lst [1, 2, 3, 4, 5, 6, 99] >>> lst.insert(100000, 42) >>> lst [1, 2, 3, 4, 5, 6, 99, 42]

No matter your concrete choice of the index argument, as long as it’s larger or equal the current size of the list, your element will be inserted at the end of the list.

However, this doesn’t make a lot of sense considering that you can use the append(element) method which adds an element at the end of the list—and is highly efficient (and readable). Read more about the append() method at my detailed blog article.

Here’s an example adding the same elements 99 and 42 to the end of the list:

>>> lst = [1, 2, 3, 4, 5, 6] >>> lst.append(99) >>> lst.append(42) >>> lst [1, 2, 3, 4, 5, 6, 99, 42]

In Python, there are usually a lot of ways to accomplish the same thing. Using the append() method to add an element to the end of the list is the better way!

9. Lists¶

A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings — and other things that behave like ordered sets — are called sequences.

Python List pop()

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

The pop() method removes the item at the given index from the list and returns the removed item.

Example

# create a list of prime numbers prime_numbers = [2, 3, 5, 7] # remove the element at index 2 removed_element = prime_numbers.pop(2) print('Removed Element:', removed_element) print('Updated List:', prime_numbers) # Output: # Removed Element: 5 # Updated List: [2, 3, 7]