Title: lists
Type: behavior Stage: resolved
Components: Versions:
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dev40573, pablogsal, steven.daprano
Priority: normal Keywords:

Created on 2019-10-12 23:54 by dev40573, last changed 2019-10-13 00:45 by steven.daprano. This issue is now closed.

File name Uploaded Description Edit dev40573, 2019-10-12 23:54
Messages (3)
msg354558 - (view) Author: DEVOR BLAKE DANIELS (dev40573) Date: 2019-10-12 23:54
when I change p[0] to 5,l[0] is also changed to 5. I use slicing to get around this ,but when dealing with lists like s[][],slicing does not work
msg354560 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-10-13 00:41
Hi Devor,

This is not a bug in Python but indeed documented behavior. When you do


you are creating a new reference to the same list ([2,3,4]), so changing one changes the other. If you want to make a copy you can do

p = l.copy()
msg354561 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-10-13 00:44
This is not a bug, it is part of the design of the language. Assignment in Python does not make a copy of lists, or any other object. In your sample code, p and l are two names for the same list, like "Devor Blake Daniels" and "dev40573" are two names for the same person (you).

You can use slicing to make a copy of the list, or the copy module, or the list constructor.

I don't understand your comment "when dealing with lists like s[][],slicing does not work".
Date User Action Args
2019-10-13 00:45:55steven.dapranosetnosy: + pablogsal
2019-10-13 00:44:16steven.dapranosetnosy: + steven.daprano
messages: + msg354561
2019-10-13 00:41:44pablogsalsetnosy: - pablogsal
2019-10-13 00:41:09pablogsalsetstatus: open -> closed

nosy: + pablogsal
messages: + msg354560

resolution: not a bug
stage: resolved
2019-10-12 23:54:11dev40573create