Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: key-value-pairs-dynamically-in-python

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: key-value-pairs-dynamically-in-python
Showing posts with label key-value-pairs-dynamically-in-python. Show all posts
Showing posts with label key-value-pairs-dynamically-in-python. Show all posts

Python Adding new key-value pairs

Python Adding new key-value pairs



Adding new key-value pairs

You can store as many key-value pairs as you want in a dictionary, until your computer runs out of memory. To add a new key-value pair to an existing dictionary give the name of the dictionary and the new key in square brackets, and set it equal to the new value.
This also allows you to start with an empty dictionary and add key-value pairs as they become relevant.

#Adding a key-value pair
alien_0 = {'color': 'green', 'points':5}

alien_0['x'] = 0
alien_0['y'] = 25
alien_0['speed'] = 1.5
#Adding to an empty dictionary
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
print(alien_0)

Screenshot 

python key value