Python Forum
2D Array/List OR using variables in other variable names?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2D Array/List OR using variables in other variable names?
#2
A multidimensional array, is just an array of arrays. The error you get, is because you're trying to append an item to the first element of a list that doesn't have any items.
>>> table = []
>>> for row in range(5):
...   table.append([])
...   for col in range(10):
...     table[row].append(col)
...
>>> table
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> import pprint
>>> pprint.pprint(table)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
Reply


Messages In This Thread
RE: 2D Array/List OR using variables in other variable names? - by nilamo - Apr-14-2018, 04:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Advancing Through Variables In A List knight2000 0 547 May-13-2023, 03:30 AM
Last Post: knight2000
  functional LEDs in an array or list? // RPi user Doczu 5 1,713 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Split string using variable found in a list japo85 2 1,352 Jul-11-2022, 08:52 AM
Last Post: japo85
  Create array of values from 2 variables paulo79 1 1,134 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,398 Nov-16-2021, 12:47 PM
Last Post: jefsummers
Question Calling on a Variable using other Variables jacknewport 4 2,055 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  An IF statement with a List variable dedesssse 3 8,497 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Create variable and list dynamically quest_ 12 4,574 Jan-26-2021, 07:14 PM
Last Post: quest_
  LIST or ARRAY Comparison and change of value nio74maz 0 1,728 Dec-21-2020, 05:52 PM
Last Post: nio74maz
Question Matching variable to a list index Gilush 17 6,037 Nov-30-2020, 01:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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