0
Can any one find the easy way for insert a number in a list with correct order?
I need this to compare my answer with others
9 Respuestas
+ 2
1) Please don't spam. Questions will usually be answered within a couple of minutes. If you keep posting "Please answer, please answer" this is rather annoying.
2) To answer your question: The fastest and most efficient way is to use "insort" from the bisect module because it is optimized for this task.
Here's a sample code that sorts ten random integers in a list:
from bisect import insort
from random import randint
a = [] # create empty list
for i in range(10):
insort(a, randint(1, 100)) # create random number and insert it at the correct position
print(a)
# sample output: [3, 5, 19, 23, 35, 49, 52, 54, 56, 87]
However this will only work if the list is sorted (or empty) at the beginning. Insort won't work for lists that aren't sorted.
0
Please specify your language
Not to do on Sololearn Q&A
https://code.sololearn.com/W26q4WtwSP8W/?ref=app
0
Give me sort answer
0
Answer me?
0
I need in python
0
I need for all kinds of inputs not only for 5
0
I need answer for my question for all kinds of inputs
0
Anna your answer is correct