In this tutorial, we will learn writing the python program to insert an element at the given position of an array (list in case of python) and print the array (list).
Problem Statement
Our problem statement is, for a given array(list) we have to add an element at the given position (index).
For example:
Case 1: if the given array (list) is [1, 2, 3, 4] and the user gives input 9 to add at index 2.
The output should be [1, 2, 9, 3, 4].
Case 2: if the given array (list) is [9, 2, 4, 8] and the user gives input 10 to add at index 1.
The output should be [9, 10, 2, 4, 8].
Our logic to insert an element at a given location of an array(list)
- Our program will take two inputs from the user, one is the elements and the other is the location ( position or index) of the element where the user wants the element to insert.
- Then, use insert() built-in method with the user inputs as an argument to insert new element at given location. After insertion print the output.
Python code to insert an element at a given location of an array(list)
Program 1: Using the insert() Method
This is the most straightforward method provided by Python’s list data structure to insert elements at a specific position. insert() Method: The insert()
method is used to place the element at the desired index. The elements at and beyond this index are shifted rightward.
# given array (list)
arr = [1, 2, 3, 4, 5]
num=int(input("Enter a number to insert in array : "))
index=int(input("Enter a index to insert value : "))
if index >= len(arr):
print("please enter index smaller than",len(arr))
else:
# insering element ‘num’ at ‘index’ position
arr.insert(index, num)
print("Array after inserting",num,"=",arr)
Output 1:
Enter a number to insert in array : 5
Enter a index to insert value : 2
Array after inserting 5 = [1, 2, 5, 3, 4, 5]
Explanation:
For the given array (list) [1, 2, 3, 4, 5], the user inputs element 5 to add at the index 2 of the array (list). using the insert() method, our program will easily add element 5 at the desired location of the list and returns the updated list.
Output 2:
Enter a number to insert in array : 5
Enter a index to insert value : 2
Array after inserting 5 = [1, 2, 5, 3, 4, 5]
Explanation:
For the given array (list) [1, 2, 3, 4, 5], the user inputs element 10 to add at the index 4 of the array (list). using the insert() method, our program will easily add element 10 at the desired location of the list and returns the updated list.
Program 2: Using Slicing
This method involves using slicing to construct a new list with the new element included at the desired position. Python lists can be sliced to create sublists. The new list is constructed by concatenating the part before the desired index, the new element (as a list), and the part after the index.
arr = [1, 2, 3, 4, 5]
num=int(input("Enter a number to insert in array : "))
index=int(input("Enter a index to insert value : "))
new_arrays = arr[:index] + [num] + arr[index:]
print("Array after inserting",num,"=",new_arrays)
Output
Enter a number to insert in array : 6
Enter a index to insert value : 2
Array after inserting 6 = [1, 2, 6, 3, 4, 5]
Each of these methods leverages Python’s flexible and dynamic handling of lists to insert elements. The choice of method depends on the specific needs for clarity, performance, and memory usage in your application.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…