[Tutor] Lists in lists
Kent Johnson
kent37 at tds.net
Sun Sep 17 02:49:06 CEST 2006
Brian van den Broek wrote:
> Kent Johnson said unto the world upon 16/09/06 04:35 PM:
>> Brian van den Broek wrote:
>>> You say you are new to Python. Well, it might not now be obvious why
>>> dictionaries are especially useful, but they are *central* to the
>>> pythonic approach. The sooner you become comfortable with them, the
>>> better (IMHO).
>> I agree that dicts are extremely useful, but I don't think they add
>> anything in this case unless there is actually a need for keyed access.
>> A list of lists (or tuples) seems very appropriate to me. A good
>> alternative might be a list of Bunches.
>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
>>
>> Kent
>
>
> Hi Kent and all,
>
> I should have included the reason why I thought a dict might be better
> here. (I did send it in a private email after the post.)
>
> A lot of ways I could imagine the time-line data being used might
> involve wanting to access some one year, rather than the entire time-line.
Yes, I was a bit hasty in denouncing dicts, the best data structure does
depend entirely on how it is to be used, and we don't know enough about
this application to know.
> >>> print timeline_data[800][0]
>
> seems *way* better than something like:
>
> >>> for year_data in timeline_data_as_list_of_lists:
> ... if year_data[0] == 800:
> ... print year_data[1]
> ... break
>
> which would be what the original list structure seems to require.
The thing is, though, how will you know that 800 is a valid year? You
need a list of valid years. If you get that list from the dict keys, and
iterate that, you haven't really gained anything over a list of tuples.
Maybe you have a lot of items and the user enters a year and you want to
print out the data you have on the year...
Kent
More information about the Tutor
mailing list