Dictionary Data Type in Python
Python’s dictionaries are dictionaries with the following properties;
- Having kind of hash table type
- Work like associative arrays
- Work like hashes found in Perl
- contains key-value pairs.
A dictionary key type can be of any type but most key types are numbers or strings.
Python Code
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/python dict = {} dict['T4'] = "This is T4" dict['Tutorials'] = "This is Tutorials" tinydict = {'name': 'ayesha','code':12345, 'dept': 'Managing'} print dict['T4'] # Prints value for 'T4' key print dict['Tutorials'] # Prints value for Tutorials key print tinydict # Prints complete dictionary print tinydict.keys() # Prints all the keys print tinydict.values() # Prints all the values |
Result
1 2 3 4 5 6 | <b>$python main.py</b> This is T4 This is Tutorials {'dept': 'Managing', 'code': 12345, 'name': 'ayesha'} ['dept', 'code', 'name'] ['Managing', 12345, 'ayesha'] |