[TriLUG] OT-ish: Python array slicing question
Nathan Yinger via TriLUG
trilug at trilug.org
Mon Dec 12 12:58:47 EST 2022
There is a difference: a[:] creates a copy.
a = [1, 2, 3, 4, 5]
b = a
c = a[:]
a[0] = 7
print(b) # [7, 2, 3, 4, 5]
print(c) # [1, 2, 3, 4, 5]
~Nathan
On Mon, Dec 12, 2022 at 11:18 AM Brian via TriLUG <trilug at trilug.org> wrote:
> Hi Gang,
>
> I figure there's probably someone in the group that can answer a
> question for me.
>
> I'm maintaining some Python code written by people no longer with the
> company. I'm also a Python newbie myself, so now and then I come across
> something that doesn't make sense and eludes my Google-fu.
>
> Given an array:
> a = [1, 2, 3, 4, 5]
>
> Is there any difference whatsoever in these two conventions:
>
> b = a[:]
> b = a
>
> A simple experiment in an interactive session doesn't seem to show any
> difference, and my understanding of array slicing says there shouldn't
> be any difference, so why would anyone write the former instead of the
> latter?
>
> Thanks,
> -Brian
> --
> This message was sent to: nathan.yinger at gmail.com <nathan.yinger at gmail.com
> >
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web :
> https://www.trilug.org/mailman/options/trilug/nathan.yinger%40gmail.com
> Welcome to TriLUG: https://trilug.org/welcome
More information about the TriLUG
mailing list