Python dictionary with keys having multiple inputs
Prerequisite : Python-Dictionary.
How to create a dictionary where a key is formed using inputs?
Let us consider an example where have an equation for three input variables, x, y and z. We want to store values of equation for different input triplets.
Example 1:
# Python code to demonstrate a dictionary # with multiple inputs in a key. import random as rn # creating an empty dictionary dict = {} # Insert first triplet in dictionary x, y, z = 10 , 20 , 30 dict [x, y, z] = x + y - z; # Insert second triplet in dictionary x, y, z = 5 , 2 , 4 dict [x, y, z] = x + y - z; # print the dictionary print ( dict ) |
Example 2: Let’s get access to the keys.
Let us consider a dictionary where longitude and latitude are the keys and the place to which they belong to is the value.
# dictionary containing longitude and latitude of places places = {( "19.07'53.2" , "72.54'51.0" ): "Mumbai" , \ ( "28.33'34.1" , "77.06'16.6" ): "Delhi" } print (places) print ( '\n' ) # Traversing dictionary with multi-keys and crearing # different lists from it lat = [] long = [] plc = [] for i in places: lat.append(i[ 0 ]) long .append(i[ 1 ]) plc.append(places[i[ 0 ], i[ 1 ]]) print (lat) print ( long ) print (plc) |
Now that the keys(latitude, longitude) and values(place) are stored in a list, we can access it easily.
Recommended Posts:
- Python | Remove multiple keys from dictionary
- Python | Initialize dictionary with multiple keys
- Python | Check if given multiple keys exist in a dictionary
- Taking multiple inputs from user in Python
- Python | Add new keys to a dictionary
- Python | Minimum value keys in Dictionary
- Python Dictionary | keys() method
- Python | Add keys to nested dictionary
- Python | Count keys with particular value in dictionary
- Python | Get dictionary keys as a list
- Python | Get the number of keys with given value N in dictionary
- Python | Grouping dictionary keys by value
- Python | Get all tuple keys from dictionary
- Python | Get total keys in dictionary
- Python | Extract specific keys from dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.