I am writing a simple collection abstraction in python using Bisect function. The problem here is that when I use insort function to add the data in the list, the function returns None. But when i use BISECT.BISECT(lst,ele), the function returns the value which would be the index of the list where the element could be inserted if desired.
# Python program to implement collection abstraction
# using Bisect Algorithm
# The program would add an element into the SORTED LIST"
# which would be the input
# The program would test whether the element is in the collection
# And also, to return the element of the collection if it is there
from bisect import bisect
from bisect import insort_right
the_list = []
def add(lst,ele):
return insort_right(lst,ele)
#def test(lst,ele)
#def remove(lst,ele):
print("Enter the size of the list:")
N = int(input())
for x in range(N):
x = input("")
the_list.append(x)
the_list.sort()
print(the_list)
print("Enter the element to be added in the list")
element = input("")
add_element = add(the_list,element)
print("The element is added in collection at location:", add_element)
insortdoesn’t return a value because it alters the list itself. Generally, Python functions that use side effects this way don’t return values. If you want to return the modified list fromadd, do this:But there’s really no point in doing so, since the returned list is the same as the list that was passed in. The calling context already has access to that list, so there’s no need to return it, and doing so is a bit unidiomatic.