1

I have a list a = [1,2,3,4,5]. And I have a function, say, Func(x). I know that if I do Func(a) then the reference of a will be passed into Func(x). And if I do Func(a[:]), a new list will be created and passed into Func(x).

So my question is: Is it possible to only pass the first three elements into Func(x) by reference, like Func(a) (I don't want to pass the whole list a into function due to certain reason)? If I do Func(a[:4]) a new list will be created and that's what I want to avoid.

The only way I can think about is to pass a and the indexes into Func(x), like Func(a, start, end).

stanleyli
  • 1,427
  • 1
  • 11
  • 28

2 Answers2

1

There is no way to create a 'window' on a list, no.

Your only options are to create a slice, or to pass in start and end indices to the function and have the function honour those.

The latter is what the bisect module functions do for example; each function takes a lo and hi parameter that default to 0 and len(list) respectively:

def func(lst, lo=0, hi=None):
    if hi is None:
        hi = len(lst)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Why not create a second argument so that Func (a) becomes Func (a, n) where a is the reference to your array and n is the position in the array to which you want to evaluate to?

Something like:

Func (a, 2)

With that example the first three elements are evaluated.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189