3

just a stupid question out of curiosity.

Lets say there's a list with elements as

fruits_lst = ['apples','oranges','bananas','guavas']

Now is it possible to create separate separate dictionaries but with the names as the names of elements in the list?

desired result:

apples=dict()
oranges=dict()
bananas=dict()
guavas=dict()

I know one could easily do it by looking at the names of the elements, but what i want to achieve is somehow the program picks up the element names while iterating through it and then creates empty dictionaries with the same names. Is it possible? Kindly guide me through..

Aman Singh
  • 1,111
  • 3
  • 17
  • 31
  • 2
    People often ask here how to create variables named from strings, and it is perfectly possible. But it's nearly always not helpful. – khelwood Dec 20 '18 at 09:07
  • @khelwood oh but why do you say so? Pardon me, but I'd like to know more. Could you guide through as to why is that not helpful? – Aman Singh Dec 20 '18 at 09:08
  • @AmanSingh Makes the code unreadable and hard to debug. – Vineeth Sai Dec 20 '18 at 09:09
  • 6
    @AmanSingh Because variables names are there for you to refer to variables in the code. Either you know them when you're writing the code, in which case you can create them explicitly; or you don't, in which case you'll find it difficult to make use of them when you have created them. – khelwood Dec 20 '18 at 09:11

3 Answers3

5

Use dict instead of globals

Use of global variables is not recommended practice. Much cleaner and easily maintainable is a dictionary of dictionaries. For example, using a dictionary comprehension:

fruits_lst = ['apples', 'oranges', 'bananas', 'guavas']

d = {fruit: {} for fruit in fruits_lst}

Then access a particular inner dictionary via d['apples'], d['oranges'], etc.

See this answer for an explanation why this is useful, and here for another example.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • I tried this but then I had to access each of the fruit as d['apples'] which is also correct but i was wondering if there was any way to access apples directly as a dictionary. But thanks for the help – Aman Singh Dec 20 '18 at 09:14
  • 3
    @AmanSingh, No, use `d['apples']`. It may seem cumbersome to a new programmer, but you shouldn't see this as inefficient or undesirable. Experienced programmers know that large numbers of variables are harder to maintain and more likely to lead to errors. – jpp Dec 20 '18 at 09:16
  • 2
    This is the reasonable approach to this problem. The data has a structure, it is reflected here as the similar dicts are grouped together, which allows to treat them together, rather than having a bunch of globals mixed among the others. – Thierry Lathuille Dec 20 '18 at 09:18
2

Everything in Python is a dictionary, even the scope. You can use globals() for it:

fruits_lst = ['apples','oranges','bananas','guavas']
globals().update({name : dict() for name in fruits_lst})
apples["foo"] = 10
print(apples)

Results are:

{'foo': 10}
Psychzander
  • 107
  • 14
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

Here's a short way, but let me warn you this isn't a good way to defining variables.

fruits_lst = ['apples','oranges','bananas','guavas']

for fruit in fruits_lst:
    globals()[fruit] = dict()

Now you can access them directly,

>>> apples
{}
>>> oranges
{}

and so on...

Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34