Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slice creates new objects?
#1
Can anybody help me understand this -
In a function, when I slice the passed list, it creates a new list instead of affecting the callers passed-list.
So in the code below, when I passed 'a' to the function as 'x', and the function slices 'x', it created a new list instead of affecting 'a'.
This seems different behavior specifically for slices... and I just saw that if I execute "a=[1,2]; a=a[1:]", the second 'a' is a new list also.

def foo1 (x):

    print("list / ID received by function:", x, id(x))
    x = x[1:]
    print("list / ID after function's slice:", x, id(x))

    return x

a =  ["alpha", "beta"]
print("list / ID original:", a, id(a))

b = foo1 (a)
print("list / ID returned:", b, id(b))
print("list / ID original:", a, id(a))

>>> 
list / ID original: ['alpha', 'beta'] 55720416
list / ID received by function: ['alpha', 'beta'] 55720416
list / ID after function's slice: ['beta'] 55720496
list / ID returned: ['beta'] 55720496
list / ID original: ['alpha', 'beta'] 55720416
>>>
Larz60+ write Jul-20-2022, 01:40 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
I don't know what made you think otherwise, but a slice makes a new object. The original object is not affected. It doesn't matter if this happens inside or outside a function.

You may be confusing objects and variables. You have a list object ["alpha", "beta"]. You create a variable named "a" and assign it to reference the list object. "a" is not a list, it is a variable that references a list. The next line in you program could assign a = 42.

You call a function foo1() and pass your list object as an argument. You are not passing "a", you are passing the list object that is referenced by "a". Inside the function Python creates a variable named "x" that is used to reference the list object inside the function. "foo1.x" and "a" reference the same list object.

Inside the function you also create a slice. The variable "x" is reassigned to reference the slice. This does not change "a" at all because the only relationship between "a" and "foo1.x" is that they used to refer to the same object. This is no longer true. "a" still refers to the original object, but "foo1.x" now refers to a new list object.
Reply
#3
What is the problem exactly? You don't want a altered, or you do want a altered?

a = [i for i in range(10)]

def slice_me(alist, num1, num2):
    x = alist[num1:num2]
    return x

b = slice_me(a, 3, 7)
Reply
#4
(Jul-20-2022, 01:03 AM)fmr300 Wrote: and I just saw that if I execute "a=[1,2]; a=a[1:]", the second 'a' is a new list also.
If you want to update the list with a slice, you can do
a[:] = a[1:]
instead of
a = a[1:]
You could also do
del a[0]
Reply
#5
Thanks guys. I'm still ramping up on Python and was confused..... these are great answers and understood. Really appreciate the quick responses!!!!
-mark
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 841 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,442 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Slice list Moris526 1 1,654 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,105 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Pass Tuple as a Slice nagymusic 2 2,374 Dec-12-2019, 04:42 AM
Last Post: nagymusic
  Preferred way to slice a list SvetlanaofVodianova 3 2,559 Dec-09-2019, 11:50 PM
Last Post: SvetlanaofVodianova
  slice python array on condition Gigux 2 2,271 Nov-03-2019, 07:21 PM
Last Post: Larz60+
  How to append and drop to next line while slice/indexing emryscass 3 2,611 Sep-26-2019, 01:06 PM
Last Post: Malt
  Understanding slice copying in for loop yksingh1097 5 4,063 Jul-02-2018, 01:03 PM
Last Post: yksingh1097
  Assigning to string slice michaeljhuman 1 2,774 Feb-08-2018, 12:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020