moctarjallo
moctarjallo

Reputation: 1615

How to build a python dictionary from a list of keys and a list values, quickly?

I have to lists

labels = ['normal.']
percentages = [0.9936]

I want to build a dictionary from these two lists

d = {}
for k, v in enumerate(lables, percentages):
    d[k] = v

But i'm getting the error:

TypeError: 'list' object cannot be interpreted as an integer

What could be wrong here ?

Edit

Then when i get the dict, i want to perform this operation

result = [str(k) + ": " + str(v) for k, v in previous_dict]

Upvotes: 0

Views: 56

Answers (2)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

One way is to zip the two lists together, and convert the zipped object to a dictionary. After that you can iterate on dict.items() to create your list

In [158]: labels = ['normal.'] 
     ...: percentages = [0.9936]                                                                                                                                                    

In [159]: previous_dict = dict(zip(labels,percentages))  
In [159]: previous_dict                                                                                                                              
Out[159]: {'normal.': 0.9936}

In [24]: result = [str(k) + ": " + str(v) for k, v in previous_dict.items()]                                                                                                        

In [25]: result                                                                                                                                                                     
Out[25]: ['normal.: 0.9936']

Also enumerate gives you a list of tuples of type (index, element), you cannot pass it two iterators like that but you can zip the two iterators again and make a dictionary, and the code will be as follows.

For Python 3.6+ we can also use f-strings to format our string as well

In [167]: labels = ['normal.'] 
     ...: percentages = [0.9936]                                                                                                                                                    

In [169]: d = {} 
     ...: for k, v in zip(labels, percentages): 
     ...:     d[k] = v 

In [170]: d                                                                                                                                                                         
Out[170]: {'normal.': 0.9936}

In [30]: result = [f'{k}:{v}' for k, v in previous_dict.items()]                                                                                                                    

In [31]: result                                                                                                                                                                     
Out[31]: ['normal.:0.9936']

Upvotes: 2

Sheldore
Sheldore

Reputation: 39042

Here is an answer using list comprehension which you were trying to do. To get the second step working, you need to use .items() to access both, the key and the value

labels = ['normal.']
percentages = [0.9936]

previous_dict = {k:v for k, v in zip(labels, percentages)}
# {'normal.': 0.9936}

result = [str(k) + ": " + str(v) for k, v in previous_dict.items()]
# ['normal.: 0.9936']

Upvotes: 0

Related Questions