[Baypiggies] Fwd: manipulating lists question

Vikram K vikthirtyfive at gmail.com
Thu Dec 5 10:29:29 CET 2013


much simpler. Thanks to Michiel, Michael, Filip, and Monte for their
helpful responses.


On Thu, Dec 5, 2013 at 4:18 AM, Michiel Overtoom <motoom at xs4all.nl> wrote:

>
> On Dec 5, 2013, at 10:09, Vikram K wrote:
>
> > another option could have been to obtain a dictionary like so:
> > {'dog': ['NM56', 65], 'cat': ['NM123,NM234', 12]}
>
> Oh, in that case the code can become somewhat simpler:
>
> x = [['cat', 'NM123', 12], ['cat', 'NM234', 12], ['dog', 'NM56', 65]]
>
> d = {}
> for key, label, quant in x:
>     if key in d:
>         d[key][0] += ", " + label
>     else:
>         d[key] = [label, quant]
>
> print d
>
>
> I agree with Michael that the problem is somewhat underspecified, but it's
> a starting point.
>
> Greetings,
>
> --
> "If you don't know, the thing to do is not to get scared, but to learn." -
> Ayn Rand
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20131205/913b0a14/attachment.html>


More information about the Baypiggies mailing list