-1

I have a loop which generates a value_list each time it runs. At the end of each iteration I want to append all the lists into a one multi dimensional array

I have:

value_list = [apple,banana,cherry,jackfruit] in 1st iteration

value_list = [cake,cookie,biscuits] in 2nd iteration

value list = [bat,cat,swan,crow,dog] in 3rd iteration and so on...

At the end of each iteration I want a one multi dimensional array like

value_list_copy = [[apple,banana,cherry,jackfruit]] in the 1st iteration

value_list_copy = [[apple,banana,cherry,jackfruit],[cake,cookie,biscuits]] in the 2nd iteration

value_list_copy = [[apple,banana,cherry,jackfruit],[cake,cookie,biscuits],[bat,cat,swan,crow,dog]] and so on...

Please help me with the code

Rahul P
  • 2,493
  • 2
  • 17
  • 31
  • Just define `value_list_copy = []` somewhere and `value_list_copy.append` the lists on each iteration. – Ardweaden Apr 09 '20 at 09:58
  • tried this...it didn't work – Priya Ramakrishnan Apr 09 '20 at 10:02
  • Could you paste the code you have written so that it could be easier to point out the change? – coldy Apr 09 '20 at 10:06
  • it is a pretty big code...i had another problem so the code I've posted in another question of stack overflow https://stackoverflow.com/questions/61098520/how-to-control-the-values-of-other-comboboxes-using-the-value-selected-in-first – Priya Ramakrishnan Apr 09 '20 at 10:53
  • @PriyaRamakrishnan Why do you do `value_list.clear()` on each iteration? Just declare `value_list = [] ` on each iteration instead of outside. This is most likely your problem. In python, lists are objects, and when you append, you append the reference to that object. Thus, when you change it, it changes everywhere. See https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – Ardweaden Apr 09 '20 at 11:27

3 Answers3

1

Initialize value_list_copy = [] before the iterations, and for each iteration, value_list_copy.append(value_list)

Refer this https://python-reference.readthedocs.io/en/latest/docs/list/append.html

Nandu Raj
  • 2,072
  • 9
  • 20
0

Good day! Try going using .append()

Else, I found this website that explains how to process lists in detail.

In essence:

a = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
b = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
a.append(b)

Or:

a.extend(b)

Also check out this thread

Gideon Botha
  • 126
  • 10
0

At first, to simulate the example: I have created a list which I can iterate.

l1 = [['apple', 'banana', 'cherry', 'jackfruit'],
      ['apple', 'cherry', 'jackfruit', 'mango'],
      ['apple', 'banana', 'cherry', 'another_fruit']]  

Then, an empty list is created to finally make it multidimensional.

copy_l = list()

Code example below, I have converted the list to numpy at the end so that you can slice it and also show the dimension.

for l in l1:
    copy_l.append(l)

import numpy as np
copy_np_marray = np.asarray(copy_l)
print('Array - ', copy_np_marray)
print('Shape - ', copy_np_marray.shape) 

Output:

Array -  
[['apple' 'banana' 'cherry' 'jackfruit']
 ['apple' 'cherry' 'jackfruit' 'mango']
 ['apple' 'banana' 'cherry' 'another_fruit']]  

Shape - (3, 4)

coldy
  • 2,115
  • 2
  • 17
  • 28