Python Forum
python nested list assignment weird behavior
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python nested list assignment weird behavior
#3
In [1]: mat = [[0]*26]*5

In [2]: id(mat[0])
Out[2]: 140082606853896

In [3]: id(mat[1])
Out[3]: 140082606853896
As you can see the list elements are one object.
Use list comprehension instead.

mat = [0 for _ in range(26)] for range(5)]]
See this video: https://www.youtube.com/watch?v=F6u5rhUQ6dU
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: python nested list assignment weird behavior - by wavic - Jan-16-2018, 07:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  strange behavior of chess library in Python max22 1 323 Jan-18-2024, 06:35 PM
Last Post: deanhystad
  Weird ProcessPoolExecutor initializer behavior davetapley 2 1,679 Mar-13-2023, 06:49 PM
Last Post: davetapley
  List all possibilities of a nested-list by flattened lists sparkt 1 930 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,653 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Updating nested dict list keys tbaror 2 1,294 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,946 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,147 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  behavior list of lists roym 5 2,117 Jul-04-2021, 04:43 PM
Last Post: roym
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,712 May-27-2021, 02:01 AM
Last Post: MDRI
  pls help with this assignment. basic into to python class valerie_thomas 2 2,062 Nov-21-2020, 12:37 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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