Indexes of tuples in a list - python

0

Organizing information in the file

(City, City, Distance)

import csv
with open('cidades.csv', 'r') as f:
       list1 = [tuple(line.values()) for line in csv.DictReader(f)]

for i in list1:
    x=(list1[i])
    print(distancia=x[2])

To access index 3, of the tuples I copied to the list, and write to a variable?

    
asked by anonymous 10.11.2017 / 00:27

1 answer

0

I did not quite understand what you want, so please rephrase your question. But if you want to access the third index of tuples in your list of tuples, you can do it directly like this:

for i in range(len(list1)):
    x = list1[i][3]
    print(x)

Or, in a simplified way:

for t in list1:
    x = t[3]
    print(x)

The way you did it is wrong because in your for you are returning an entire tuple, not an index to index in list1 .

    
10.11.2017 / 17:16