I am trying to ouptut a list of dicts with unique user IDs. I am using the following code to achieve this:
import json
import random
import string
def random_string_generator(size=15, chars=string.ascii_uppercase + string.digits):
return '#' + ''.join(random.choice(chars) for _ in range(size))
list_users = ['user_id'] * 5
data = {}
list_json = []
for user in list_users:
data['user_id'] = random_string_generator() # function to generate a unique key
list_json.append(data)
print(list_json)
However, my code output is giving the following:
[{'user_id': '#J1FTZ3TLOITMVW6'}, {'user_id': '#J1FTZ3TLOITMVW6'}, {'user_id': '#J1FTZ3TLOITMVW6'}, {'user_id': '#J1FTZ3TLOITMVW6'}, {'user_id': '#J1FTZ3TLOITMVW6'}]
I would like to know why the list is filled with the same item over and over again?