Skip to content

Commit 4efba15

Browse files
committed
adding insertion of element into list
1 parent 0cd1de2 commit 4efba15

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

desktop.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[LocalizedFileNames]
2+
insert_element_into_list.py=@insert_element_into_list.py,0

insert_element_into_list.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Python3 program to insert an element into sorted list
2+
3+
# Function to insert element
4+
def insert(list, n):
5+
6+
# Searching for the position
7+
for i in range(len(list)):
8+
if list[i] > n:
9+
index = i
10+
break
11+
12+
# Inserting n in the list
13+
list = list[:i] + [n] + list[i:]
14+
return list
15+
16+
17+
list = [1, 2, 4]
18+
n = 3
19+
20+
print(insert(list, n))

0 commit comments

Comments
 (0)