0

I am trying to create a list of dictionary. The expected output is that for 5 Students, it should have 5 dictionary items in the list with keys being Name and Marks (in a list). The output that I get is having the information repeated 5 times for the last student in iteration. Please let me know where am I making an error.

import random
name_list=[]
name_list = ['A','B','C','D','E']
stud_dict=dict()
stud_dict_list = []
for name in name_list:
    mark_list = []
    for i in range(1,11):
        mark_list.append(random.randint(0,100))
    stud_dict['Name']=name
    stud_dict['Marks']=mark_list
    stud_dict_list.append(stud_dict)
print(stud_dict_list) 

output:
[{'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}, {'Name': 'E', 'Marks': [91, 71, 93, 33, 52, 90, 97, 98, 41, 18]}]
Jaydeep
  • 3
  • 1

2 Answers2

0

You need to empty stud_dict in every iteration.

Code:

for name in name_list:
    mark_list = []
    for i in range(1,11):
        mark_list.append(random.randint(0,100))
    stud_dict['Name']=name
    stud_dict['Marks']=mark_list
    stud_dict_list.append(stud_dict)
    stud_dict=dict() # Here we are making dictionary empty by re-intializing with new empty dictionary.

Output:

>>> stud_dict_list
[{'Name': 'A', 'Marks': [18, 67, 26, 83, 15, 4, 93, 74, 9, 69]}, {'Name': 'B', 'Marks': [11, 81, 21, 53, 70, 88, 21, 11, 81, 73]}, {'Name': 'C', 'Marks': [18, 58, 45, 45, 26, 73, 15, 25, 9, 99]}, {'Name': 'D', 'Marks': [16, 85, 81, 21, 92, 77, 44, 2, 99, 80]}, {'Name': 'E', 'Marks': [96, 66, 69, 96, 5, 56, 61, 5, 19, 95]}]
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
0

This has been asked many times but I don't have time to search for an exact dup ATM, so:

Python never copies things unless explicitely asked for. Here, you're adding the same dict to your list over and over again:

>>> import random
>>> name_list=[]
>>> name_list = ['A','B','C','D','E']
>>> stud_dict=dict()
>>> stud_dict_list = []
>>> for name in name_list:
...     mark_list = []
...     for i in range(1,11):
...         mark_list.append(random.randint(0,100))
...     stud_dict['Name']=name
...     stud_dict['Marks']=mark_list
...     stud_dict_list.append(stud_dict)
... 
>>> stud_dict
{'Name': 'E', 'Marks': [94, 51, 79, 82, 84, 76, 92, 96, 7, 52]}
>>> id(stud_dict)
139663685096344
>>> [id(item) for item in stud_dict_list]
[139663685096344, 139663685096344, 139663685096344, 139663685096344, 139663685096344]
>>> 

Since it's the very same dict, each update of the dict will of course be reflected in the list. And the solution is quite simply to create a new dict each time:

import random
name_list = ['A','B','C','D','E']
stud_dict_list = []
for name in name_list:
    mark_list = []
    for i in range(1,11):
        mark_list.append(random.randint(0,100))
    stud_dict = {'Name': name, 'Marks': mark_list}
    stud_dict_list.append(stud_dict)

You also want to read this for more on Python's variables...

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118