Python Forum
Why is dictionary length not 20?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is dictionary length not 20?
#1
Hi all,

Here's some code:

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)
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
Reply


Messages In This Thread
Why is dictionary length not 20? - by Mark17 - Oct-07-2021, 05:42 PM
RE: Why is dictionary length not 20? - by bowlofred - Oct-07-2021, 05:49 PM
RE: Why is dictionary length not 20? - by Mark17 - Oct-07-2021, 05:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: dictionary update sequence element #0 has length 1; 2 Jmekubo 4 41,001 Apr-28-2019, 07:25 PM
Last Post: Jmekubo
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 4,680 Mar-03-2018, 05:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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