How to convert a nested list into a dictionary in Python

2 Answers

0 votes
nestedlist = [[1, 'a'], [2, 'bb'], [3, 'ccc'], [4, 'dddd']]

dic = {}
for sub_list in nestedlist:
    dic[sub_list[0]] = sub_list[1]

print(dic)


'''
run:

{1: 'a', 2: 'bb', 3: 'ccc', 4: 'dddd'}

'''

 



answered Feb 10, 2025 by avibootz
0 votes
nestedlist = [['aaa', 'bbb', 'ccc', 'ddd'], ['eee', 'fff', 'ggg'], ['hhh', 'iii']]

dic = {}
for sub_list in nestedlist:
    dic[sub_list[0]] = sub_list[1:]    

print(dic)


'''
run:

{'aaa': ['bbb', 'ccc', 'ddd'], 'eee': ['fff', 'ggg'], 'hhh': ['iii']}

'''

 



answered Feb 10, 2025 by avibootz
...