Reputation: 21
I'm trying to iterate over a list of dictionaries and keeping only those with a year value in their yearID
key. Essentially, the list (statistics
) is baseball statistics and each row (dictionary) are the stats of a player during a year.
This code seems to work just fine (for VERY small lists of dictionaries), but as soon as the size of the list goes over 40 or 50, Thonny crushes:
def filter_by_year(statistics, year, yearid):
nlist = []
for dicts in statistics:
if str(dicts[yearid]) == str(year):
nlist.append(dicts)
return nlist
Upvotes: 2
Views: 1264
Reputation: 57033
Of all the methods presented so far (by prashant rana, Zaid Afzal, alec_a, and Steven Burnap), yours - the original one - is the most efficient. It becomes somewhat 3x faster if you eliminate the unnecessary conversion to the strings:
def filter_by_year(statistics, year, yearid):
nlist = []
for dicts in statistics:
if dicts[yearid] == year:
nlist.append(dicts)
return nlist
Upvotes: 2
Reputation: 11228
A filter
can also be a good way
def filter_by_year(statistics, year, yearid):
nlist = list(filter (lambda dicts: str(dicts[yearid]) == str(year), statistics))
return nlist
Upvotes: 0
Reputation: 356
You could use a generator to only grab year and keep index of dictionary being iterated.
def filter_by_year(statistics, year, yearid):
nlist = []
for i, yearid_ in enumerate(dict[yearid] for dict in statistics):
if str(yearid_) == str(year):
nlist.append(statistics[i])
return nlist
Upvotes: 0
Reputation: 9536
Depends what you mean by "efficient." Your code should work fine for large amounts of dictionaries, so I'm gonna assume you mean efficient in terms of code written.
In that case, nlist
can be simplified into a simple list comprehension:
[dicts for dicts in statistics if str(dicts[yearid]) == str(year)]
Upvotes: 2
Reputation:
I have no idea if it's more efficient, but a list comprehension is cleaner code:
return [dicts for dicts in statistics if str(dicts[yearid]) == str(year)]
Upvotes: 1