[melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread David Crisp
The following question is more along the lines of "good practice" rather than "how do you do it" . If I have a name:value list of values that I want to read into a dict for ease of lookup, lets define them as: name | value == ItemOne : 10 ItemOne : 10 ItemOne : 10 ItemOne : 10 ItemTwo

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Anthony Briggs
Sounds like you don't need the checking, since the items are all identical, *but* if the values differ for a particular key, then the behaviour will be different (earliest with checking, latest without). It you have itemOne: 10, then itemOne: 20, the checked version will have 10 for itemOne You ca

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread William ML Leslie
On 21/02/2014 9:40 am, "Anthony Briggs" wrote: > > You can also use the dict() function or dictionary comprehensions to create your dictionary: > item = dict( (key, value) for key, value in list ) > Otherwise written: item = dict(list) ___ melbourne

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Anthony Briggs
Yep, just realised that - if it's stored as a list of key, value pairs. On 21 February 2014 09:45, William ML Leslie wrote: > > On 21/02/2014 9:40 am, "Anthony Briggs" wrote: > > > > You can also use the dict() function or dictionary comprehensions to > create your dictionary: > > item = dict

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Josh Bode
Hi David, On my machine, on a simple, pathological example, it is significantly faster (around a factor of 2) to check for duplicates. However, it depends if you expect to have many duplicates. In the second example with only one duplicated item, it is faster to not check. The winner in both cas

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Ben Finney
David Crisp writes: > name | value > == > ItemOne : 10 > ItemOne : 10 > ItemOne : 10 > ItemOne : 10 > ItemTwo : 20 > ItemTwo : 20 > ItemTwo : 20 > ItemTwo : 20 > ItemThree : 30 > ItemThree : 30 > ItemThree : 30 > ItemThree : 30 If you're confident there will frequently be duplicated lin

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread David Crisp
Thanks for the thoughts! In this case I am reading in data from a SQL database query(pymssql) and feeding it into an excel spreedsheet (xlwt) Line by line which means I have to assemble the dict line by line. And yes, the name i chose in the email to represent what I was doing was generic.

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Josh Bode
If the data is in a database, and the rows are truly duplicates (i.e. keys always map to the same values) you could probably remove the duplicates via the query before loading the data into Python. The DISTINCT keyword will do this for you, i.e. SELECT DISTINCT * FROM x; Otherwise, a GROUP BY co

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Ben Finney
David Crisp writes: > In this case I am reading in data from a SQL database query(pymssql) If you want to eliminate duplicates from your query, do so with ‘SELECT DISTINCT’. Then you don't ever get the duplicate rows in the first place :-) -- \ “Ocean, n. A body of water occupying ab

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread David Crisp
Ahh :)I also meant to say that whilest the particular fields I am talking about in this question are the same, data at the end of those rows is different, and will be treated differently. I really abstracted my query away from what I am actually trying to do exactly and more along the li

Re: [melbourne-pug] Question about adding members to list : hypothetical just for interest kind of question

2014-02-20 Thread Josh Bode
If the data you need to collapse is being duplicated across multiple rows due to distinct values in other columns, you could generate that data in a separate, more focussed query. That way you would avoid having to de-duplicate that information, but you do have an additional query to maintain and f