0

If i have a variable number of arrays, how do I initialize/work on them?

x = int(input())

for i in range(x):
    dict_i = {}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    I don't think that this is possible. I'd rather go for a dict of dicts. – brillenheini Jun 24 '21 at 10:54
  • Sounds a bit like an [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - I cannot think of any reason why someone would do this. – TheEagle Jun 24 '21 at 11:07

3 Answers3

1

This would generate a list of dictionaries. You should be able to access them afterwards based on index.

x = int(input())

dictionaries = []
for i in range(x):
    dictionaries.append(dict())

print(dictionaries)
A.M. Ducu
  • 892
  • 7
  • 19
0

Simplest solution would be to use globals and setattr:

x = int(input())

for i in range(x):
    globals()[f"dict_{i}"] = {}

But I can't think of why you would want to do this at all.

TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • i was thinking of doing a all-possible-combination analysis of a set of related variable. I always start thinking of the pointer system and I end up thinking of how to do this – bramblephoenix Jun 24 '21 at 11:12
-2

you just can make it as below

x = int(input())
y= [{}]*x