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 can also use the dict() function or dictionary comprehensions to create
your dictionary:
item = dict( (key, value) for key, value in list )
or
item = {key: value for (key, value) in list}
Finally, I wouldn't go overriding the builtin list() function with your
variable name though. Call it something less generic, like item_pricing.
Hope that helps,
Anthony
On 21 February 2014 09:31, David Crisp <[email protected]> wrote:
> 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 : 20
> ItemTwo : 20
> ItemTwo : 20
> ItemTwo : 20
> ItemThree : 30
> ItemThree : 30
> ItemThree : 30
> ItemThree : 30
>
> Now, obviously there are duplicates in that list. If I use a simple
> loop such as the following:
> for each_name in list:
> item[name] = value
>
> Then I will get a dict with three pairs in it:
> ItemOne : 10
> ItemTwo : 20
> ItemThree : 30
>
> Which is what I want.
>
> Now, MY question is, is there any harm in creating the dict that way
> and looping through all those values multiple times and re-defining the
> values constantly (to the same thing). OR, should I put a check in there
> such as:
>
> if name not in item:
> # add name and its pair value to dict
>
>
> In my case I HAVE added checking in but I was wondering if it was really
> needed. Given no matter what, in either case, the resulting dict would be
> the same.
>
> Regards,
> David
>
> _______________________________________________
> melbourne-pug mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/melbourne-pug
>
>
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug