Python Forum
assign a value to a dict change all the values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assign a value to a dict change all the values
#1
hello,

i am really getting mad with these few lines of code

from random import randint

header = ["test", "1", "2", "12", "GG", "NG"]
lis = ["alpha", "beta", "gamma"]

list_p = list()
dict_p = dict.fromkeys(header)

def random():

    for item in lis:
        dict_p["test"] = item
        for key in list(dict_p.keys())[1:]:
            dict_p[key] = randint(1,600)/100.0

        list_p.append(dict_p)
        
    return list_p

print(random())
i know from here that Changing One dict value changes all values in a list and even if i feel completely dumb i am not able to apply these answers to my case

could you explain what i am doing wrong? Wall
Reply


Messages In This Thread
assign a value to a dict change all the values - by aster - Sep-18-2018, 05:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Change row values by other row values from same df JosepMaria 3 2,341 Aug-28-2021, 10:15 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020