You do not "declare" arrays or anything else in python. You simply assign to a (new) variable. If you want a multidimensional array, simply add a new array as an array element.
arr = []
arr.append([])
arr[0].append('aa1')
arr[0].append('aa2')
or
arr = []
arr.append(['aa1', 'aa2'])
Answer from ThiefMaster on Stack OverflowYou do not "declare" arrays or anything else in python. You simply assign to a (new) variable. If you want a multidimensional array, simply add a new array as an array element.
arr = []
arr.append([])
arr[0].append('aa1')
arr[0].append('aa2')
or
arr = []
arr.append(['aa1', 'aa2'])
There aren't multidimensional arrays as such in Python, what you have is a list containing other lists.
>>> arr = [[]]
>>> len(arr)
1
What you have done is declare a list containing a single list. So arr[0] contains a list but arr[1] is not defined.
You can define a list containing two lists as follows:
arr = [[],[]]
Or to define a longer list you could use:
>>> arr = [[] for _ in range(5)]
>>> arr
[[], [], [], [], []]
What you shouldn't do is this:
arr = [[]] * 3
As this puts the same list in all three places in the container list:
>>> arr[0].append('test')
>>> arr
[['test'], ['test'], ['test']]
do you guys know how to add append an array in a 2d array?
Using numpy.append() without losing array dimensions? [Mac, 2.7]
How to append 3d numpy array to a 4d array
Sounds like what you really need is a python list of 3D numpy arrays. Appending to a numpy array is possible with np.append or np.concat, but it's very expensive because it forces the entire array to be remade. Is there any reason you want a 4D array?
More on reddit.com2D array ( list ) in Python
https://www.reddit.com/r/learnpython/wiki/faq#wiki_why_is_my_list_of_lists_behaving_strangely.3F
More on reddit.comVideos
say i have array [[1,2,3,4,5]], how can i duplicate it 5 times to [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] ?