I am trying to use Python to convert listto dictionary, and I need to help come up with a simple solution. The list I would like to convert is as follows:
inv = ['apples', 2, 'oranges', 3, 'limes', 10, 'bananas', 7, 'grapes', 4]
I want to create dictionaryfrom this listwhere the elements in even positions (apples, oranges, limes, bananas, grapes) are keys, and the elements in odd positions (2, 3, 10, 7, 4) are values.
inv_dict = {'apples':2, 'oranges':3, 'limes':10, 'bananas':7, 'grapes':4}
I tried using something like enumerateto calculate the position of an element, and then if it is equal, set it as key. But then I'm not sure how to match the next number with its correct element.
source
share