Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they’ve been created.
An index refers to a position within an list.
Given, a list the task is to insert an element at specific index in given list.
Insert an element at Specific Index in List
- Insert() function
- Inserting an element at a specific position
- Inserting element at the first position of the list
- Inserting element at the end position of the list
- Insert all elements of second list at specific index in given list
1)Insert() function
Syntax:
givenlist.insert(index, element)
Parameters:
index - the position in the array at which the element must be inserted. element - the element that will be added to the list.
Return:
This method does not return a value, but instead inserts the specified element at the specified index.
2)Inserting an element at a specific position
Suppose we want to insert ‘python’ in a givenlist at index 3 we can pass arguments like
insert(3,value) where value=‘python’
Below is the implementation:
# given list givenlist = ['hello', 'this', 'is', 'BTechGeeks'] # given value value = 'python' # given index index = 3 # inserting the given value at given index givenlist.insert(index, value) # print the list print(givenlist)
Output:
['hello', 'this', 'is', 'python', 'BTechGeeks']
3)Inserting element at the first position of the list
We know that the starting index of list is 0 so we give 0 as index while inserting the given value.
Below is the implementation:
# given list givenlist = ['hello', 'this', 'is', 'BTechGeeks'] # given value value = 'python' # inserting the given value at starting position givenlist.insert(0, value) # print the list print(givenlist)
Output:
['python', 'hello', 'this', 'is', 'BTechGeeks']
4)Inserting element at the end position of the list
We know that the last index of the list is len(list)-1 to insert element at end we use len(list) as parameter.
Passing this index to insert function inserts the element at the end of the list.
Below is the implementation:
# given list givenlist = ['hello', 'this', 'is', 'BTechGeeks'] # given value value = 'python' # inserting the given value at end position givenlist.insert(len(givenlist), value) # print the list print(givenlist)
Output:
['hello', 'this', 'is', 'BTechGeeks', 'python']
5)Insert all elements of second list at specific index in given list
By slicing we can insert it easily .
Splice given list from 0 to index-1 and merge all of list2’s elements into it. Then, from index to the end of the list, merge all of the remaining elements.
Below is the implementation:
# given list givenlist = ['hello', 'this', 'is', 'BTechGeeks'] # given value givenlist2 = ['welcome', 'to', 'python'] # given index index = 2 # inserting the given value using slicing givenlist = givenlist[:index]+givenlist2+givenlist[index:] # print the list print(givenlist)
Output:
['hello', 'this', 'welcome', 'to', 'python', 'is', 'BTechGeeks']
Related Programs:
- python how to add an element in list append vs extend
- python program to find the minimum index of a repeating element in an array list
- python how to find all indexes of an item in a list
- python how to check if an item exists in list
- cpp how to get element by index in list
- python how to find an element in tuple by value
- python program to find the odd occurring element in an array list