-2

I need to create empty arrays inside an array in order to fill each one of them differently in a loop later on but I couldn't figure out the correct syntax. I tried something like this:

 verilerimiz = [arrayname1[], arrayname2[], arrayname3[], arrayname4[], arrayname5[]]

Bu it gives a syntax error. I really appreciate some help.

Aslıhan Yılmaz
  • 23
  • 1
  • 2
  • 6
  • have you tried using `numpy.empty ` https://docs.scipy.org/doc/numpy/reference/generated/numpy.empty.html – Hari Krishnan Aug 28 '18 at 06:47
  • 4
    Just remove the names: `verilerimiz = [[], [], [], ...]` – Klaus D. Aug 28 '18 at 06:47
  • 2
    You probably want to read about [dictionaries](https://docs.python.org/3/tutorial/datastructures.html#dictionaries): `v = {'a1': [], 'a2': [], 'a3': []}`, so you can access e.g. `a2` by name: `v['a2'].append(123)`. – randomir Aug 28 '18 at 06:48

3 Answers3

1

i think this will help you (if i understand your question correctly):

verilerimiz = list()
for i in range(5):
    verilerimiz.append(list())

# adding 'some value' to `arrayname1`
verilerimiz[0].append("some value")
xeptore
  • 63
  • 8
0

here's my example

a1 = []
a2 = []
a3 = [1,2,3]

fa = [a1,a2,a3]
print(len(fa[2]))

also, check this out for referencing: Access item in a list of lists

Stanley
  • 2,434
  • 18
  • 28
0

Everything is an object in Python. And Lists can accept any type of data. So List can accept Lists too. Using this you can just place lists in a list.

array1 = [] # main array

array2 = [] 
array3 = []
array4 = []

array1 = [array2, array3, array4] # Or you can just give [] instead of variable names

This will create the following

array1
[[], [], []]
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34