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. Show
Example1: Inserting an Element to the ListPython3
Output: [1, 2, 3, 4, 10, 5, 6, 7] ['z', 'a', 'b', 'c', 'd', 'e']Example 2: Error of insert() MethodPython3
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 ElementPython3
Output: [1, 2, 13, 3, 4, 5, 6]Example 4: Inserting a Tuple to the ListPython3
Output: [1, 2, (4, 5, 6), 3, 4, 5, 6]
Article Tags :
Python
Python-Built-in-functions python-list python-list-functions Practice Tags :
python-list Python List insert() Negative IndexYou 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 BeginningThe 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 EndOkay, 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] |