[Tutor] extracting lists from lists of lists

Bill Mill bill.mill at gmail.com
Tue Sep 14 13:05:44 CEST 2004


Nik,

Take a look at dictionaries -
http://docs.python.org/tut/node7.html#SECTION007400000000000000000 .
They let you create a mapping from keys to data. See:

In [34]: stuff = {'name': 'john', 'age': '88', 'gender': 'M'}

In [35]: stuff['name']
Out[35]: 'john'

In [36]: stuff['age']
Out[36]: '88'

In [37]: (stuff['name'], stuff['age'], stuff['gender'])
Out[37]: ('john', '88', 'M')

Hope this helps.

Peace
Bill Mill

On Tue, 14 Sep 2004 12:52:09 +0200, nik <my.mailing.lists at noos.fr> wrote:
> hi,
> 
> I have a class which is just a holder for a data structure, and for some
> reason I've decided to hold the data in the following form;
> 
> class myData:
>     data = [  ["name", ""], ["age", ""], ["gender", ""] ]
> 
> I'm not totally sure it's the best way, but it strikes me as something
> that can be easily manipulated into maps etc. I had started out with
> class myData:
>     name = ""
>     age = ""
>     gender = ""
> 
> but I found that I had to put most of those items into lists to do
> anything with them.
> 
> So apart from any advice on holding data like that, I was wondering if
> there's any cool way to get the values into a tuple?
> 
> ie [  ["name", "john"], ["age", "88"], ["gender", "M"] ]   -> ("john",
> "88", "M")
> 
> I can use a for loop probably, but I've seen people do some very clever
> stuff with slicing and multiple assignment (which I'm still getting to
> grips with, but loving). Any suggestions?
> 
> Suggestions for holding the data differently initially are welcome too
> (I'd like the data to be obvious to users that they shouldn't change the
> names, but be able to easily add/change/manipulate the values).
> 
> thanks,
> nik
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list