Referencing Items in a List of Tuples

Rune Strand rune.strand at gmail.com
Sat Feb 24 21:20:15 EST 2007


On Feb 25, 3:01 am, rshep... at nospam.appl-ecosys.com wrote:
>   In my case, I have a list of 9 tuples. Each tuple has 30 items. The first
> two items are 3-character strings, the remaining 28 itmes are floats.
>
>   I want to create a new list from each tuple. But, I want the selection of
> tuples, and their assignment to the new list, to be based on the values of
> the first two items in each tuple.
>
>   If I try, for example, writing:
>
> for item in mainlist:
>     if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
>       ec.Append(mainlist[item][2:])
>
> python doesn't like a non-numeric index.
>

try this instead:
for item in mainlist:
     if item[0] == 'eco' and item[1] == 'con':
       ec.append(item[2:])

if you want numeric adressing, try:
for i in range(len(mainlist)):
   if mainlist[i][0] == 'eco'  etc.








More information about the Python-list mailing list