-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.

8
  • 1
    You just wanna print it out that way with linebreaks?? If so, do for key, val in dct.items(): print(key, val)
    – juanpethes
    Commented Aug 3, 2023 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.
    – pho
    Commented Aug 3, 2023 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.
    – pho
    Commented Aug 3, 2023 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. Commented Aug 3, 2023 at 21:10
  • 1
    You extract them by using mydict[key] when you need the list. E.g. another_function(mydict['aaa'])
    – Barmar
    Commented Aug 3, 2023 at 22:00

2 Answers 2

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']
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']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.