Home > front end >  Python memory use in loop function call vs variable assigment
Python memory use in loop function call vs variable assigment

Time:06-02

Lets say I have a dataframe which I want to transform into list of dictionaries and loop over them.

What would be more efficient?

df = some_dataframe
df_as_list = df.to_dict('records')

for i in df_as_list:
    requests.get(i['url'])

or

df = some_dataframe
for i in df.to_dict('records'):
    requests.get(i['url'])

Does the second example call to_dict() on every loop?

CodePudding user response:

There is only one call to to_dict. The for loop is basically equivalent to

t = iter(df.to_dict('records'))
while True:
    try:
        i = next(t)
    except StopIteration:
        break

    requests.get(i['url'])

It doesn't matter whether you assign the result of df.to_dict to a variable or not, because the result is simply passed to iter, and that iterable is passed to next multiple times.

  • Related