Skip to content

Instantly share code, notes, and snippets.

@audiodude
Created January 5, 2024 03:50
Show Gist options
  • Save audiodude/ca43f20ecc0b4709f85bb187da096375 to your computer and use it in GitHub Desktop.
Save audiodude/ca43f20ecc0b4709f85bb187da096375 to your computer and use it in GitHub Desktop.
Increment random value from a dictionary
fruits={'apple': 0.49, 'banana': 0.69, 'peach': 1.29, 'pear': 1.49, 'watermelon': 2.99}
Incrementing banana by 1
fruits={'apple': 0.49, 'banana': 1.69, 'peach': 1.29, 'pear': 1.49, 'watermelon': 2.99}
import random
fruits = {
'apple': 0.49,
'banana': 0.69,
'peach': 1.29,
'pear': 1.49,
'watermelon': 2.99,
}
print(f'{fruits=}')
key = random.choice(list(fruits.keys()))
print(f'Incrementing {key} by 1')
fruits[key] += 1
print(f'{fruits=}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment