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

16th Dec 2018, 12:19 PM
Abinesh.B
Abinesh.B - avatar
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.
16th Dec 2018, 12:54 PM
Anna
Anna - avatar
16th Dec 2018, 12:29 PM
Gordon
Gordon - avatar
0
Please specify your language Not to do on Sololearn Q&A https://code.sololearn.com/W26q4WtwSP8W/?ref=app
16th Dec 2018, 12:21 PM
Gordon
Gordon - avatar
0
Give me sort answer
16th Dec 2018, 12:23 PM
Abinesh.B
Abinesh.B - avatar
0
Answer me?
16th Dec 2018, 12:24 PM
Abinesh.B
Abinesh.B - avatar
0
I need in python
16th Dec 2018, 12:25 PM
Abinesh.B
Abinesh.B - avatar
0
I need for all kinds of inputs not only for 5
16th Dec 2018, 12:32 PM
Abinesh.B
Abinesh.B - avatar
0
I need answer for my question for all kinds of inputs
16th Dec 2018, 12:36 PM
Abinesh.B
Abinesh.B - avatar
0
Anna your answer is correct
16th Dec 2018, 1:34 PM
Abinesh.B
Abinesh.B - avatar