how to create a new dictionary in for loop?

Viewed 12353

In python 3.5.1

og_dict = {'name':'user1', 'salary':'1k'}
og_dict_list =[]
for i in range(2,5):
    og_dict['salary'] = str(i)+'k'
    og_dict_list.append(og_dict)
for og_dict_obj in og_dict_list:
    print(og_dict_obj)

Above code produces following output :

{'name': 'user1', 'salary': '4k'}
{'name': 'user1', 'salary': '4k'}
{'name': 'user1', 'salary': '4k'}

But I am expecting below output :

{'name': 'user1', 'salary': '2k'}
{'name': 'user1', 'salary': '3k'}
{'name': 'user1', 'salary': '4k'}

Apparantely python modifies the existing dictionary only and does not create a new one. What can I do to solve this issue?

Is there exist a way to create a new dictionary object from existing one? Is there any other collection which is more suitable for this?

2 Answers
Related