Home > other >  How do I update only values in a dictionary?
How do I update only values in a dictionary?

Time:11-30

I am trying to make an anonymous voting program in Python for my condo building. There are 128 units. I want to assign each unit a randomly generated value.

I generated the list of units but the key:value pairs are the same. It looks like Python's update() method requires that input the key for every value I want to update. That is way too long of a process.

I am stuck.

import random

def create_anonymous_votes():
    unit_numbers = {}

#updates the entire dictionary for unit_numbers
    unit_numbers.update({x: x for x in range(1,129)})

#here is what I was thinking would assign and update each key with a random value, but when I print it only prints the original dictionary.

for unit in range(1,129):
    unit_numbers[0] = random.randint(1,1000)
    

print(unit_numbers)

CodePudding user response:

you can iterate your dictionary and set random values as :

dict = {'key1': value1, 'key2': value2, 'key3': value3}

for key in dict:
    dict[key] = value

https://realpython.com/iterate-through-dictionary-python/

CodePudding user response:

import random #Import random 
random_vote_list = [] #creates an empty list
for i in range(1,128):
    random_vote_list.append(random.randint(1,1000)) 

print(random_vote_list)

I think this is what you wanted it will print a list of 128 units each with a random value!

To navigate this list use random_vote_list[0] will give the first one and so on

CodePudding user response:

One problem in your code is that you are changing item index 0 in your loop. Change the code in the loop to unit_numbers[unit] = random.randint(1, 1000) and it should do what you want. But! Don't forget that your function should return something, and that you need to actually call your function. The code you posted in the question produces a NameError.

All that said... instead of making an empty dictionary, then updating it with numbers you then have to update, you could make everything inside the dictionary comprehension:

import random

def create_anonymous_votes():
    """Create a random number for each unit."""
    return {x: random.randint(1, 1000) for x in range(1, 129)}

unit_numbers = create_anonymous_votes()
print(unit_numbers)

CodePudding user response:

You could use random.choices and rebuild the whole dictionary in one go:

from random import choices 

unit_numbers = dict(enumerate(choices(range(1,1001),k=128),1))

print(unit_numbers)
{1: 698, 2: 401, 3: 460, 4: 691, 5: 365, 6: 325, 7: 882, 8: 970, 
 9: 828, 10: 122, 11: 205, 12: 173, 13: 253, 14: 68, 15: 899, 16: 528, 
 17: 308, 18: 550, 19: 15, 20: 53, 21: 834, 22: 70, 23: 156, 24: 588, 
 25: 93, 26: 759, 27: 443, 28: 241, 29: 480, 30: 112, 31: 593, 32: 576, 
 33: 915, 34: 433, 35: 960, 36: 742, 37: 262, 38: 242, 39: 929, 40: 496, 
 41: 287, 42: 426, 43: 741, 44: 675, 45: 154, 46: 640, 47: 44, 48: 667, 
 49: 233, 50: 336, 51: 116, 52: 600, 53: 717, 54: 214, 55: 923, 56: 391, 
 57: 157, 58: 420, 59: 913, 60: 911, 61: 727, 62: 24, 63: 807, 64: 212, 
 65: 456, 66: 814, 67: 514, 68: 631, 69: 2, 70: 456, 71: 596, 72: 771, 
 73: 734, 74: 740, 75: 848, 76: 300, 77: 494, 78: 896, 79: 149, 80: 797, 
 81: 271, 82: 589, 83: 679, 84: 990, 85: 253, 86: 281, 87: 648, 88: 738, 
 89: 179, 90: 350, 91: 871, 92: 73, 93: 589, 94: 938, 95: 653, 96: 413, 
 97: 260, 98: 210, 99: 718, 100: 822, 101: 861, 102: 597, 103: 699, 
 104: 352, 105: 315, 106: 469, 107: 707, 108: 276, 109: 857, 110: 622, 
 111: 50, 112: 935, 113: 659, 114: 543, 115: 851, 116: 157, 117: 549, 
 118: 229, 119: 66, 120: 163, 121: 659, 122: 337, 123: 952, 124: 789, 
 125: 182, 126: 566, 127: 770, 128: 636}
  • Related