Generation

generate functionSun, 11 Dec 2022

A list of arbitrary length is given. It is necessary to write code, which on the basis of the original list will compose a nesting level dictionary of the same length as the original list. Examples of how the program works: my_list = ['2018-01-01', 'yandex', 'cpc', 100] Result: {'2018-01-01': {'yandex': {'cpc': 100}}} my_list = ['a', 'b', 'c', 'd', 'e', 'f'] Result: {'a': {'b': {'c': {'d': {'e': 'f'}}}}} Translated with www.DeepL.com/Translator (free version)

def make_dict(my_list): result = {} if len(my_list) > 2: result[my_list[0]] = make_dict(my_list[1:]) else: result[my_list[0]] = my_list[1] return result my_list = ['a', 'b', 'c'] print(make_dict(my_list))

Questions about programming?Chat with your personal AI assistant