[Baypiggies] Fwd: manipulating lists question

Michiel Overtoom motoom at xs4all.nl
Thu Dec 5 10:18:13 CET 2013


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      





More information about the Baypiggies mailing list