-1

I have a list of values that looks like this : values = [2.5,3.6,7.5,6.4], Is it possible to create several dictionaries with the values from this list and the same key value for all items, so the final output will look like this:

list_of_dic = [{'interest rate':2.5}, {'interest rate'}:3.6, {'interest rate':7.5}, {'interest rate':6.4}]
Camilla
  • 111
  • 10
  • 2
    Of course it is possible. Did you try to write code to solve the problem? What happened when you tried? You are expected to try first before asking, and show your attempt. Please read [ask]. Also, when you have difficulty solving a problem, you should try to break it down into smaller steps. Do you know how to create a single dictionary of the sort that you want, from a single value? Do you know how to apply a process to each item of a list? What happens if you put those two techniques together? – Karl Knechtel Dec 28 '21 at 23:02
  • 2
    Also, why do you want a list of dictionaries like that? The key doesn't seem to be adding any useful information... is there a slightly more complex example where doing this makes sense? – JeffUK Dec 28 '21 at 23:07
  • 2
    Does this answer your question? [Create a dictionary with list comprehension](https://stackoverflow.com/questions/1747817/create-a-dictionary-with-list-comprehension) – JeffUK Dec 28 '21 at 23:07

2 Answers2

1

You can achieve this by using list comprehension:

return [{'interest rate': val} for val in values]

or

a = []
    for val in values:
        a.append({
            'interest rate':val
        })
        
    return a

Both do the same thing

Srishruthik Alle
  • 500
  • 3
  • 14
-1

Sure, just make a second table "labels" with your labels.

Then :

def f(labels, values):
    l = [] # here is the result list you want to fill with dicts
    foreach i in range(0, len(values)): # for each value
        d = dict{} # create dict
        d[values[i]] = labels[i] # put your key and value in it
        l.append(d) # add it to your result list
    return l # return the result list