Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list syntax
#2
>>> x = [1,2,3]
>>> y = x
>>> z = x[::]
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
>>> z
[1, 2, 3]
>>> x[0] = 4
>>> x
[4, 2, 3]
>>> y
[4, 2, 3]
>>> z
[1, 2, 3]
>>> id(x)
140222264135304
>>> id(y)
140222264135304
>>> id(z)
140222264168648
in your example y = x does not create new list, y reference the same object as x. In my example you can see they have the same id (i.e. they are the same object). I showed one of the possible methods to copy/clone a list. There are also other options see https://stackoverflow.com/a/2612815/4046632
Reply


Messages In This Thread
list syntax - by blazersnake - Feb-28-2018, 04:35 PM
RE: list syntax - by buran - Feb-28-2018, 05:13 PM
RE: list syntax - by Robo_Pi - Feb-28-2018, 05:20 PM
RE: list syntax - by Mario793 - Feb-28-2018, 05:22 PM
RE: list syntax - by Robo_Pi - Feb-28-2018, 05:59 PM
RE: list syntax - by blazersnake - Feb-28-2018, 05:46 PM
RE: list syntax - by buran - Feb-28-2018, 07:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Syntax Hitso 4 2,341 May-26-2020, 10:51 AM
Last Post: hussainmujtaba
  What is the correct syntax for list items that need to contain a quotation mark, etc? KaisoArt 7 3,601 Sep-14-2019, 05:26 AM
Last Post: buran
  syntax error on list.sort() jjordan33 3 3,264 Jul-10-2019, 04:57 PM
Last Post: jefsummers
  Help with list Syntax. jarrod0987 2 2,672 Jan-13-2018, 01:11 PM
Last Post: jarrod0987

Forum Jump:

User Panel Messages

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