Reputation: 68
I'm a new python learner and i have come here with a doubt
i have a dictionary my_dict with a 2 lists inside it with keys dic_keys and dic_values now i want to insert the popped elements from dic_values into dic_keys and also to create a new popped_ele key with all the popped elements as a list
my_dict = {'dic_keys':[],'dic_values':[1,2,3]}
popped_ele = my_dict['dic_values'].pop()
my_dict['dic_keys'].append(popped_ele)
my_dict['dic_popped'] = [].append(popped_ele)
this is what i expected
my_dict {'dic_keys': [3], 'dic_values': [1, 2], 'dic_popped': [3]}
but this is what i got my_dict {'dic_keys': [3], 'dic_values': [1, 2], 'dic_popped': None}
Upvotes: 0
Views: 31
Reputation: 513
you can use
my_dict = {'dic_keys': [], 'dic_values': [1, 2, 3]}
my_dict['dic_popped']=[]
popped_ele = my_dict['dic_values'].pop()
my_dict['dic_keys'].append(popped_ele)
my_dict['dic_popped'].append(popped_ele)
print(my_dict)
Upvotes: 1