I have a generator which yields a new dictionary whenever called. Although despite the generator producing a new dictionary the list keeps appending the same dictionary over and over again. My code is like follows.
def dictionary_generator():
dictionary = {}
...
yield dictionary
def my_function(var):
my_list = []
for i in range(var):
my_list.append(next(dictionary_generator))
return my_list
My generator yields the following dictionary for example,
{
'name': 'john',
'age': 26,
'work':'politician'
}
{
'name': 'tom',
'age': 34,
'work':'cricketer'
}
{
'name': 'jimmy',
'age': 99,
'work':'actor'
}
Although my list will look something like this
[
{
'name': 'jimmy',
'age': 99,
'work':'actor'
},
{
'name': 'jimmy',
'age': 99,
'work':'actor'
},
{
'name': 'jimmy',
'age': 99,
'work':'actor'
}
]
Note:
The code is simplified and narrowed to the lines of code where the bug exists
What I have tried:
I have tried different methods of appending the list but all worked as the same way as I described