Oct-07-2021, 05:42 PM
Hi all,
Here's some code:
If I run this several times, len(new_dict) changes and it's never 20 as I would expect.
What did I do wrong?
Thanks,
Mark
Here's some code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
test_keys = [] for i in range ( 0 , 20 ): n = random.randint( 1 , 20 ) test_keys.append(n) #print(randomlist) numbers = range ( 0 , 20 ) #test_values = [number for number in numbers] test_values = [] for j in numbers: m = random.randint( 1 , 20 ) test_values.append(m) new_dict = {test_keys[k]: test_values[k] for k in range ( len (test_keys))} print ( f 'Dictionary has been compliled with length {len(new_dict)}.' ) print ( f 'Length of test_keys is {len(test_keys)}' ) print ( f 'Length of test_values is {len(test_values)}' ) print ( f 'test_values is:{test_values}' ) print ( f 'test_keys is:{test_keys}' ) print (new_dict) |
What did I do wrong?
Thanks,
Mark