[Tutor] how to convert list of lists to dict

Alan Gauld alan.gauld at btinternet.com
Tue May 17 19:10:10 CEST 2011


"Brad Hudson" <brad.hudson at gmail.com> wrote

> dictionary object. Can someone help me to convert data similar to 
> the
> following using two different examples of a list comprehension and a 
> for
> loop?

Given that you want more than one method I assume
this is a homwework assignment rather than a practical
problem? If so we would prefer that you showed us what
you tried and we can then help you fix it rather than
just offer you solutions from which you will learn little.

> Data is similar to the following:
> [['name=server1', 'state=active', 'ncpu=8', 'mem=8589934592',
> 'uptime=5784399'], ['name=server2', 'state=active', 'ncpu=32',
> 'mem=34359738368', 'uptime=5416796'], ['name=server3', 
> 'state=inactive',
> 'ncpu=8', 'mem=8589934592', 'uptime=']]
>
> I'd like to convert the list data into a dictionary similar to the
> following:
> node['server1'] = {'state' : 'active', 'ncpu' : 8, 'mem' : 
> 8589934592,
> 'uptime' : 5784399}
> node['server2'] = {'state' : 'active', 'ncpu' : 32, 'mem' : 
> 34359738368,
> 'uptime' : 5416796}
> node['server3'] = {'state' : 'inactive', 'ncpu' : 8, 'mem' : 
> 8589934592,
> 'uptime' : None}

How did you think it should be done?
Maybe if we start from there?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list