-1

I have a dictionary named Asset_Class — the keys are the following:

dict_keys(['Equity Fund', 'Structure Products', 'Unclassified', 'Equity', 'Alternatives', 
           'Derivatives', 'Fixed Income'])

I need to create a list as per below

listt = [Asset_Class['Equity'], Asset_Class['Equity Fund'], Asset_Class['Fixed Income']]

How can I create a loop to populate the list with the full dictionary name and key?

martineau
  • 119,623
  • 25
  • 170
  • 301
Mariano
  • 295
  • 1
  • 4
  • 13
  • 4
    is `listt` not a list? what's the problem? –  Apr 04 '22 at 19:26
  • Adding onto what @enke said, could you provide concrete input / outputs for what you're trying to accomplish? – BrokenBenchmark Apr 04 '22 at 19:26
  • Are you just trying to get a list of the values? then `Asset_Class.values()` should get you what you want – joshmeranda Apr 04 '22 at 19:31
  • You can't create a list that has the name of the dictionary and key like that. It would be possible to create one that had the value associated with each one of those keys in the `Asset_Class` dictionary — is that what you meant? – martineau Apr 04 '22 at 19:42
  • The name the dictionary is assigned to will be one of the keys in `locals()` but I don't know how you would go about deciding which one. – wwii Apr 04 '22 at 20:24
  • Possibly related: [Getting the name of a variable as a string](https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string) – wwii Apr 04 '22 at 20:27

2 Answers2

0

You need a list containing all the key/value pairs in the dict?

listt = [Asset_Class['Equity'], Asset_Class['Equity Fund'], Asset_Class['Fixed Income']]

Use the dict.items() method, like this:

listt = []
for key, value in Asset_Class.items():
    listt.append(key)
    listt.append(value)

This is the result:

>>> dictionary = {"key": "value"}
>>> print(dictionary.items())
dict_items([('key', 'value')])
>>> listt = []
>>> for key, value in dictionary.items():
        listt.append(key)
        listt.append(value)
>>> print(listt)
['key', 'value']
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • If you are trying to store the key-value pairs I think you mean to use `dict.items()` rather than `dict.values()` – joshmeranda Apr 04 '22 at 19:35
0

It's unclear exactly what you want to achieve (and whether it's even possible). Here are a couple of ways of creating a list that contain elements that are possible — they both create the result using something called a list comprehension which is a very succinct method of creating lists programmatically.

Note that none of the list contain the name of the full dictionary (Asset_Class) as you have in your question (because that's impossible).

Asset_Class = {
    'Equity Fund': 'value1',
    'Structure Products': 'value2',
    'Unclassified': 'value3',
    'Equity': 'value4',
    'Alternatives': 'value5',
    'Derivatives': 'value6',
    'Fixed Income': 'value7'
}

# First possibility.
listt = [Asset_Class[key] for key in ('Equity', 'Equity Fund', 'Fixed Income')]
print(listt)  # -> ['value4', 'value1', 'value7']

# Second possibility.
listt = [(key, value) for key, value in Asset_Class.items()
            if key in ('Equity', 'Equity Fund', 'Fixed Income')]
print(listt)  # -> [('Equity Fund', 'value1'), ('Equity', 'value4'), ('Fixed Income', 'value7')]
martineau
  • 119,623
  • 25
  • 170
  • 301