Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append 2d empty lists
#3
(Oct-19-2020, 09:18 PM)bowlofred Wrote: Your first line has not made a list of 5 different lists inside, it's made a list with 5 copies of the same list inside.

>>> l = [[]] * 5
>>> l
[[], [], [], [], []]
>>> [id(x) for x in l]
[4464943816, 4464943816, 4464943816, 4464943816, 4464943816]
Instead you want to create a new list 5 different times. Perhaps:

>>> l = [[] for i in range(5)]
>>> l
[[], [], [], [], []]
>>> [id(x) for x in l]
[4464944008, 4464944648, 4464944456, 4464944328, 4464944200]
>>> l[2].append(3)
>>> l
[[], [], [3], [], []]

Thank you very much! This helps out a lot!
Reply


Messages In This Thread
Append 2d empty lists - by NMMST - Oct-19-2020, 08:56 PM
RE: Append 2d empty lists - by bowlofred - Oct-19-2020, 09:18 PM
RE: Append 2d empty lists - by NMMST - Oct-19-2020, 09:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Better way to append frames from a video to multiple lists? Balaganesh 0 228 May-13-2021, 07:37 AM
Last Post: Balaganesh
  Lists + Empty Lists Pytho13 15 1,004 Mar-26-2021, 08:52 PM
Last Post: pythonprogrammer1101935
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 707 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append list to empty array SchroedingersLion 1 881 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  append elements into the empty dataframe jazzy 0 1,128 Sep-26-2018, 07:26 AM
Last Post: jazzy

Forum Jump:

User Panel Messages

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