There are two issues with your program
- It returns an empty dict
{}
instead of the dict you created - You have still not incorporated the filter function
I have to eliminate the cities which have less than 10,000 people.
- You shouldn't name a variable to a built-in
The fixed code
def Dictionary():
d={}
with open("population.tsv") as f:
for line in f:
(key, value)=line.split()
key = int(val)
#I have to eliminate the cities which have less than 10,000 people
if key < 10000:
d[int(key)]=val
#return {}
#You want to return the created dictionary
return d
#list=Dictionary()
# You do not wan't to name a variable to a built-in
lst = Dictionary()
print(lst)
Note you could also use the dict
built-in by passing a generator expression or straightforward dict comprehension (If using Py 2.7)
def Dictionary():
with open("population.tsv") as f:
{k: v for k,v in (map(int, line.split()) for line in f) if k < 10000}
#If using Py < Py 2.7
#dict((k, v) for k,v in (map(int, line.split()) for line in f) if k < 10000)