[Tutor] Dynamic naming of lists
Peter Otten
__peter__ at web.de
Tue Mar 31 17:50:01 CEST 2015
Ian D wrote:
> Thanks I will look into these. The data going in is a list like
> this:['broadcast', '"d8on"', 'broadcast', '"d11on"']
>
> With the output beng something like this.
>
> lst_0 = ['broadcast', '"d8on"']
>
> lst_0 = ['broadcast', '"d11on"']
Is that a typo, did you mean
lst_1 = ['broadcast', '"d11on"']
? If so us a list. Complete example:
>>> flat_pairs = ['broadcast', '"d8on"', 'broadcast', '"d11on"']
>>> it = iter(flat_pairs)
>>> pairs = list(zip(it, it))
>>> pairs
[('broadcast', '"d8on"'), ('broadcast', '"d11on"')]
You can then access individual pairs by providing an index into the pairs
list:
>>> pairs[0]
('broadcast', '"d8on"')
>>> pairs[1]
('broadcast', '"d11on"')
> I have managed to use a dictionary as advised in a post on StackOverflow;
> not quite completed it as I am overwriting my lists each time. From your
> experience is it better to pursue the dictionary route or the zip tuple
> option. I am in python2.7
A dictionary is typically used when the keys are not as regular as a
sequence of integers, e. g. to map a user name to an email address or a word
to a list of the positions where it occurs in a text.
More information about the Tutor
mailing list