Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

insert in a sorted list Python

# list insert bisect Python. insert value in a sorted list. 
import bisect

a = [1, 2, 4, 5]
value = 3

bisect.insort(a, value)
print(a)
# [1, 2, 3, 4, 5]

# Alternative implementation
idx = bisect.bisect(a, value)
print(idx)
# 2
a.insert(idx, value)
print(a)
# [1, 2, 3, 4, 5]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #insert #sorted #list #Python
ADD COMMENT
Topic
Name
4+8 =