[Solved] How to flip list to dictionary python


This question is a bit ambiguous as you did not provide example input or output. I will assume that you are going to ask the user multiple times to enter in an item and how many they need separated by a comma. If the input changes, then I will edit my answer.

dictionary = {}
item, numberOfItems = raw_input("Enter your Shopping List: ").title().split(',')
dictionary[item] = numberOfItems

This is assuming that no matter what, your input comes in the form of:

banana,3
carrot,7
pack of gum,9

Edit
Since I see what the input is, I can answer this better.

The input string someone will input follows this pattern:

item1,3,item2,7,item3,20,item4,5

So we need to split the input every 2 items.

my_list = raw_input("Enter your Shopping List: ").title().split(',')
composite_list = [my_list[x:x+2] for x in range(0, len(my_list),2)]

This should set composite_list as such.

[[item1,3], [item2,7], [item3,20], [item4,5]]

Now all we need to do is loop through composite_list

for pair in composite_list:
    print('({}:{})'.format(pair[0], pair[1]))

The output will now look like:

(item1:3)

3

solved How to flip list to dictionary python