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


Messages In This Thread
Slice creates new objects? - by fmr300 - Jul-20-2022, 01:03 AM
RE: Slice creates new objects? - by deanhystad - Jul-20-2022, 02:14 AM
RE: Slice creates new objects? - by Pedroski55 - Jul-20-2022, 03:23 AM
RE: Slice creates new objects? - by Gribouillis - Jul-20-2022, 06:06 AM
RE: Slice creates new objects? - by fmr300 - Jul-20-2022, 12:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 878 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,513 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Slice list Moris526 1 1,675 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,139 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Pass Tuple as a Slice nagymusic 2 2,392 Dec-12-2019, 04:42 AM
Last Post: nagymusic
  Preferred way to slice a list SvetlanaofVodianova 3 2,583 Dec-09-2019, 11:50 PM
Last Post: SvetlanaofVodianova
  slice python array on condition Gigux 2 2,299 Nov-03-2019, 07:21 PM
Last Post: Larz60+
  How to append and drop to next line while slice/indexing emryscass 3 2,644 Sep-26-2019, 01:06 PM
Last Post: Malt
  Understanding slice copying in for loop yksingh1097 5 4,116 Jul-02-2018, 01:03 PM
Last Post: yksingh1097
  Assigning to string slice michaeljhuman 1 2,797 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