How to create a dictionary out of a list of lists in python?


How to create a dictionary out of a list of lists in python?



Let's suppose I have the following list made out of lists


list1 = [['a','b'],['a'],['b','c'],['c','d'],['b'], ['a','d']]



I am wondering if there is a way to convert every element of list1 in a dictionary where all the new dictionaries will use the same key. E.g: if ['a']
gets to be {'a':1}, and ['b'] gets to be {'b':2}, I would like for all keys a the value of 1 and for all keys b the value of 2. Therefore, when creating the dictionary of ['a','b'], I would like to turn into {'a':1, 'b':2}.


list1


['a']


{'a':1}


['b']


{'b':2}


a


1


b


2


['a','b']


{'a':1, 'b':2}



What I have found so far are ways to create a dictionary out of lists of lists but using the first element as the key and the rest of the list as the value:



Please note that's not what I am interested in.
The result I would want to obtain from list1 is something like:


list1


dict_list1 = [{'a':1,'b':2}, {'a':1}, {'b':2,'c':3}, {'c':3,'d':4}, {'b':2}, {'a':1,'d':4}]



I am not that interested in the items being that numbers but in the numbers being the same for each different key.





{'b':3,'c':4} ?
– Bear Brown
Jul 3 at 6:55



{'b':3,'c':4}





My mistake, {'b':2,'c':4}
– Marisa
Jul 3 at 6:57


{'b':2,'c':4}





c should be 3 and d should be 4 ?
– sachin dubey
Jul 3 at 6:58





@sachindubey I just add that I am not as interested in the numbers themselves but in being sure that every key in the dictionary have the same value in all the dictionaries.
– Marisa
Jul 3 at 7:00




3 Answers
3



Using chain and OrderedDict you can do auto mapping


from itertools import chain
from collections import OrderedDict

list1 = [['a','b'],['a'],['b','c'],['c','d'],['b'], ['a','d']]
# do flat list for auto index
flat_list = list(chain(*list1))
# remove duplicates
flat_list = list(OrderedDict.fromkeys(flat_list))
mapping = {x:flat_list.index(x)+1 for x in set(flat_list)}

[{e: mapping[e] for e in li} for li in list1]



You need to declare your mapping first:


mapping = dict(a=1, b=2, c=3, d=4)



Then, you can just use dict comprehension:


[{e: mapping[e] for e in li} for li in list1]
# [{'a': 1, 'b': 2}, {'a': 1}, {'b': 2, 'c': 3}, {'c': 3, 'd': 4}, {'b': 2}, {'a': 1, 'd': 4}]





thats sth i learned today!
– hsnsd
Jul 3 at 7:04





You could potentially create mapping automatically. e.g. mapping = {a:i for i,a in enumerate(string.ascii_lowercase, 1)}.
– Paul Rooney
Jul 3 at 7:04



mapping


mapping = {a:i for i,a in enumerate(string.ascii_lowercase, 1)}





Unfortunately mapping assignment can only be formulated after having looked to the list content.
– guidot
Jul 3 at 7:05


mapping



Here a try with ord() also it will work for both capital and lower letters :


ord()


[{e: ord(e)%32 for e in li} for li in list1]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails