Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Dictionary in Python
- # Creating a dictionary
- dict = {
- 'John': 'john.smith@gmail.com',
- 'Mary': 'mary.johnson@gmail.com',
- 'Richard': 'richard.jackson@gmail.com',
- 'Cindy': 'cindy.mcgregor@gmail.com',
- 'Jessica': 'jessica.jones@gmail.com'
- }
- # Accessing a single element
- print("John's address is", dict['John'])
- # Deleting a key-value
- del dict['Jessica']
- # Adding a key-value pair
- dict['Mark'] = 'mark.zuckerberg@gmail.com'
- # Find the count of dict elements
- print('\nThere are {} contacts in the address-book\n'.format(len( dict)))
- # Accessing the whole dictionary
- for name, address in dict.items():
- print('Contact {} at {}'.format(name, address))
- # Checking if a value is the dictionary
- if 'Cindy' in dict:
- print("\nCindy's address is", dict['Cindy'])
Add Comment
Please, Sign In to add comment