capyman1701
capyman1701

Reputation: 37

Python: better to make new temporary list or make multiple index accesses to same list?

I have a 3d list, and I need to compare each element at the lowest level to the max of the list it's in.

if lst[i][j][k] ==  max(lst[i][j]): ...

is this bad practice? it's worse on performance, the only alternative I can think of is

templst = lst[i][j]
if templst[k] == max(templst): ...

but this is worse on memory afaik.

Upvotes: 0

Views: 36

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83557

but this is worse on memory afaik

I think you assume that the new variable is a copy of the list, but it isn't. When you do templst = lst[i][j], the only additional memory used is for a reference to the existing list.

Upvotes: 2

Related Questions