Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List repetition
#4
Here just some test,so can you think of we they are different.
>>> lst_1 = [[1,2,3]] * 3
>>> lst_2 = [[1,2,3] for i in range(3)]
>>> lst_1
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> lst_2
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> 
>>> lst_1[0].append(4)
>>> lst_2[0].append(4)
>>> lst_1
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> lst_2
[[1, 2, 3, 4], [1, 2, 3], [1, 2, 3]]
>>> 
>>> [id(i) for i in lst_1]
[23028560, 23028560, 23028560]
>>> [id(i) for i in lst_2]
[65702968, 65711560, 65708120]
>>> 
>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
   Return the identity of an object.
   
   This is guaranteed to be unique among simultaneously existing objects.
   (CPython uses the object's memory address.)
Reply


Messages In This Thread
List repetition - by ashwin - May-24-2017, 10:35 AM
RE: List repetition - by buran - May-24-2017, 11:43 AM
RE: List repetition - by wavic - May-24-2017, 11:46 AM
RE: List repetition - by snippsat - May-24-2017, 12:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Way to avoid repetition? Tuxedo 5 2,927 Feb-16-2021, 08:02 PM
Last Post: Tuxedo
  question about you want repetition this task loczeq 6 3,394 Mar-05-2020, 08:35 PM
Last Post: loczeq
  Random nr. no repetition & printing multiple lines Joey 7 2,862 Feb-05-2020, 04:23 PM
Last Post: Larz60+
  About generating N integer numbers without repetition Otbredbaron 3 3,916 Jan-30-2018, 12:08 PM
Last Post: Otbredbaron

Forum Jump:

User Panel Messages

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