Python: to deal the case when the returned value is a large structure [closed]

StackOverflow https://stackoverflow.com/questions/23682912

  •  23-07-2023
  •  | 
  •  

Question

if the returned value is a very large structure, like a list with millions of elements, how to deal such cases?

like

x=set[()]
def f(x):
     ....
     ....
     # now x has millions of elements
     return x
Was it helpful?

Solution

  1. you meant

    x = set()

  2. the function acting on x does not need to return x, it can modify the passed value.

  3. there is no duplication of data when you return a list or any other object... just because the value passed is not copied.

  4. you are affected by the premature optimization syndrome since you are trying to solve an optimization problem before testing that the problem actually holds.

Example of function modifying a set:

>>> def f(x):
...   x.add(42)
... 
>>> x = set([13])
>>> x
set([13])
>>> f(x)
>>> x
set([42, 13])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top