Simplified Dictionary using comprehensions

  dictionary-comprehension, python

everyone i’d like to know a way to simplify the following code with a dictcomprehension:

import random

vowel = "aeiou"
consonants = "bcdfghjklmnpqrstvwxyz"
hand = {}

for i in range(numVowels):
        x = VOWELS[random.randrange(0,len(VOWELS))]
        hand[x] = hand.get(x, 0) + 1
        
for i in range(numVowels, n):    
        x = CONSONANTS[random.randrange(0,len(CONSONANTS))]
        hand[x] = hand.get(x, 0) + 1

Source: Python Questions

LEAVE A COMMENT