Re: Best way to insert sorted in a list

2017-09-08 Thread Stephan Houben
Op 2017-09-08, logonve...@gmail.com schreef : > On Saturday, June 18, 2011 at 2:23:10 AM UTC+5:30, SherjilOzair wrote: >> There are basically two ways to go about this. >> One is, to append the new value, and then sort the list. >> Another is to traverse the list, and insert the new value at the >>

Re: Best way to insert sorted in a list

2017-09-08 Thread Steve D'Aprano
On Sat, 9 Sep 2017 04:39 am, logonve...@gmail.com wrote: > On Saturday, June 18, 2011 at 2:23:10 AM UTC+5:30, SherjilOzair wrote: ...^^ You're replying to something six years old. Its doubtful the original poster is still reading. >> What has the community to say about t

Re: Best way to insert sorted in a list

2017-09-08 Thread logonveera
On Saturday, June 18, 2011 at 2:23:10 AM UTC+5:30, SherjilOzair wrote: > There are basically two ways to go about this. > One is, to append the new value, and then sort the list. > Another is to traverse the list, and insert the new value at the > appropriate position. > > The second one's complex

Re: Best way to insert sorted in a list

2011-06-19 Thread Vito De Tullio
SherjilOzair wrote: > There are basically two ways to go about this. [...] > What has the community to say about this ? What is the best (fastest) > way to insert sorted in a list ? a third way maybe using a SkipList instead of a list on http://infohost.nmt.edu/tcc/help/lang/python/examples/py

Re: Best way to insert sorted in a list

2011-06-17 Thread Steven D'Aprano
On Fri, 17 Jun 2011 13:53:10 -0700, SherjilOzair wrote: > What has the community to say about this ? What is the best (fastest) > way to insert sorted in a list ? if you're doing repeated insertions into an already sorted list, there's no question that the bisect module is the right way to do it

Re: Best way to insert sorted in a list

2011-06-17 Thread Chris Torek
In article I wrote, in part: >>Appending to the list is much faster, and if you are going to >>dump a set of new items in, you can do that with: [...] In article Ethan Furman wrote: >> a.append(large_list) > ^- should be a.extend(large_list) Er, right. Posted in haste (had to get

Re: Best way to insert sorted in a list

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 3:48 PM, Chris Torek wrote: > If len(large_list) is m, this is O(m).  Inserting each item in > the "right place" would be O(m log (n + m)).  But we still > have to sort: > >    a.sort() > > This is O(log (n + m)), hence likely better than repeatedly inserting > in the corre

Re: Best way to insert sorted in a list

2011-06-17 Thread Ethan Furman
Chris Torek wrote: Appending to the list is much faster, and if you are going to dump a set of new items in, you can do that with: # wrong way: # for item in large_list: #a.append(item) # right way, but fundamentally still the same cost (constant # factor is much smaller

Re: Best way to insert sorted in a list

2011-06-17 Thread Chris Torek
In article SherjilOzair wrote: >There are basically two ways to go about this. >One is, to append the new value, and then sort the list. >Another is to traverse the list, and insert the new value at the >appropriate position. > >The second one's complexity is O(N), while the first one's is O(N *

Re: Best way to insert sorted in a list

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 3:02 PM, Shashank Singh wrote: > Correct me if I am wrong here but isn't the second one is O(log N)? > Binary search? > That is when you have an already sorted list from somewhere and you > are inserting just one new value. Finding the position to insert is O(log n), but t

Re: Best way to insert sorted in a list

2011-06-17 Thread Ethan Furman
SherjilOzair wrote: What has the community to say about this ? What is the best (fastest) way to insert sorted in a list ? Check out the bisect module. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list

Re: Best way to insert sorted in a list

2011-06-17 Thread Shashank Singh
On Sat, Jun 18, 2011 at 2:23 AM, SherjilOzair wrote: > There are basically two ways to go about this. > One is, to append the new value, and then sort the list. > Another is to traverse the list, and insert the new value at the > appropriate position. > > The second one's complexity is O(N), while