I am trying to write a function that returns a list of cities from a text file so that when the function is called, you can put an index after it, and it will return the corresponding city.
Example:
citylist('MYFILE.txt')[3]
So far, I have
def citylist(filename):
assert type(filename)==str
with open(filename) as FileObject:
for line in FileObject:
q=line.split('\t')
print q[12],
There are 500 complaints. After I split each string (complaint) into a list, the city name is the 13th index in the list. But I am stuck as all I can get it to do is print all of the city names as non data types that cannot be indexed.
You could create a list and return it:
Alternatively, and more succinctly:
where I’ve built the list using a list-comprehension in this last example.