-2

What is the best way to create multiple list from a dictionary? My dictionary looks like this:

mydict = {'aaa': ['111', '222', '333'], 'bbb': ['444', '555']}

I'm trying to make lists like this:

aaa ['111', '222', '333']
bbb ['444', '555']
...

I've tried

result = []
For key, value in mydict.item()
    result[key].append(value)

This returns a type error.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    You just wanna print it out that way with linebreaks?? If so, do `for key, val in dct.items(): print(key, val)` – juanpethes Aug 03 '23 at 20:53
  • Lists don't contain key-value pairs, so your line `result[key].append(...)` is does not make sense -- `result` is a list, `result[key]` won't exist (unless `key` is an integer and `result` contains at least `key+1` elements, and `result[key]` isn't a list because `result` was initialized as an __empty__ list. – Pranav Hosangadi Aug 03 '23 at 20:57
  • Moreover, your dictionary is invalid. Did you mean for the backslash to be a `]`? And the _"I am trying to make lists like this"_ does not make sense because `aaa ['111', '222', '333']` is _not_ a list. The latter part of that line is a list, but `aaa` isn't part of that list. Are you asking how to create a list named `aaa` that contains those three strings, and another named `bbb` that contains those two strings? If so, why? Creating variables with dynamic names is [rightly frowned upon.](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Pranav Hosangadi Aug 03 '23 at 21:01
  • Juanpethes, great that prints what I'm looking for but I need to be able to pass one of them to another function. – user22335241 Aug 03 '23 at 21:10
  • Pranav, yes that is supposed to be a ]. I'm trying to pass those "lists" to another function. – user22335241 Aug 03 '23 at 21:12
  • You can access those lists through the dictionary directly. Why are you trying to extract them into a variable? Please add some more details about what you're trying to do – Pranav Hosangadi Aug 03 '23 at 21:17
  • Pranav, ultimately I'm trying to create multiple lists from the dictionary to be able to pass the lists to another function. I'm not trying to put them onto a variable, I just need to extract the lists to be available for another function. Thanks for the help. – user22335241 Aug 03 '23 at 21:32
  • 1
    You extract them by using `mydict[key]` when you need the list. E.g. `another_function(mydict['aaa'])` – Barmar Aug 03 '23 at 22:00

2 Answers2

0

This is as close as you could get to what you would want, the issue in your code is the way you're initializing the result list and accessing its elements. You can get what you would want is by using a loop to iterate through the dictionary items. The list would then be populated with tuples that would have the key and value pair from your dictionary, then you want to loop through result to print your output.

mydict = {'aaa': ['111', '222', '333'], 'bbb': ['444', '555']}
result = []

for key, value in mydict.items():
    result.append((key, value))

# Printing the result
for key, value in result:
    print(key, value)

Output:

aaa ['111', '222', '333']
bbb ['444', '555']
BigHeroSus
  • 23
  • 4
0

Turning the dictionary keys into variables is possible but not something that you would want to do in a real program:

>>> globals().update(mydict)
>>> aaa
['111', '222', '333']
>>> bbb
['444', '555']

If you only want the printed output to look like that, then use a for loop to print:

for i in mydict.items(): print(*i)

aaa ['111', '222', '333']
bbb ['444', '555']

If you want to manipulate the lists by name, you could create a dictionary subclass that processes its keys as attribute names:

class fieldDict(dict):
    __getattr__ = dict.get
    __setattr__ = dict.__setitem__

lists = fieldDict(mydict)

lists.aaa   # ['111', '222', '333']  
lists.bbb   # ['444', '555']
Alain T.
  • 40,517
  • 4
  • 31
  • 51