Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append 2d empty lists
#2
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], [], []]
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
  Syntax when initializing empty lists Mark17 2 1,323 Jun-02-2022, 04:09 PM
Last Post: Mark17
  Better way to append frames from a video to multiple lists? Balaganesh 0 1,820 May-13-2021, 07:37 AM
Last Post: Balaganesh
  Lists + Empty Lists Pytho13 15 5,380 Mar-26-2021, 08:52 PM
Last Post: pythonprogrammer1101935
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,523 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append list to empty array SchroedingersLion 1 2,143 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,187 Mar-20-2019, 08:01 PM
Last Post: stillsen
  append elements into the empty dataframe jazzy 0 2,070 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