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?
#1
I'm trying to do one of two things:
1- .append() to a 2d array.
OR
2- have multiple arrays, e.g. array1, array2, then use array+intvar.append()

I have tried defining my array (list) with array = [][] and array = [[]].
I have tried appending to array[1].append("test").

I have also tried:
array1 = []
intvar = 1
exec("array"+str(intvar)).append("test"))
Error: AttributeError: 'NoneType' object has no attribute 'append'

Ideally, solution 1 would be better than solution 2, because I don't need to manually create X arrays. However, I would like an explanation for both methods.

Thank you in advance.
Reply
#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
#3
(Apr-14-2018, 04:03 PM)nilamo Wrote: 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([])
That makes sense, but when I tried it, I instead wrote for row in range(1, 5): since it can't access row[0].

Now it works. Thank you.
Reply
#4
(Apr-14-2018, 10:46 PM)IAMK Wrote: I instead wrote for row in range(1, 5):
Then you're doing something else wrong. What I shared was working code, copied directly from an interactive session. A list with any elements will always have an item at element 0.
Reply
#5
(Apr-16-2018, 03:31 PM)nilamo Wrote: Then you're doing something else wrong.
No, I've left [0] empty. It's fine :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advancing Through Variables In A List knight2000 0 521 May-13-2023, 03:30 AM
Last Post: knight2000
  functional LEDs in an array or list? // RPi user Doczu 5 1,604 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Split string using variable found in a list japo85 2 1,301 Jul-11-2022, 08:52 AM
Last Post: japo85
  Create array of values from 2 variables paulo79 1 1,092 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,364 Nov-16-2021, 12:47 PM
Last Post: jefsummers
Question Calling on a Variable using other Variables jacknewport 4 2,004 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  An IF statement with a List variable dedesssse 3 8,265 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Create variable and list dynamically quest_ 12 4,417 Jan-26-2021, 07:14 PM
Last Post: quest_
  LIST or ARRAY Comparison and change of value nio74maz 0 1,703 Dec-21-2020, 05:52 PM
Last Post: nio74maz
Question Matching variable to a list index Gilush 17 5,893 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