Use something other than a <br> string for the <br> dict key in Python

I often use strings for dict keys, but ...

stock = {
    "orange": 3,
    "apple": 1,
}

Actually, you can use other than character strings

d = {
    1: "Numerical value",
    date(2016, 1, 1): "date"
    (1, 2): "Tuple",
    None: "None"
}

Key specifications

--Any hashable and immutable object can be used --It is OK even if multiple types are mixed in one dict --None is OK


Examples of types that can be used for keys


Practical example


Practical example

--Tuple and compound key --Use numeric keyed dict instead of list


Tuple and compound key

stock = {
    ("Tokyo branch", "orange"): 3,
    ("Tokyo branch", "apple"): 1,
    ("Nagoya Branch", "orange"): 8,
}
stock["Nagoya Branch", "apple"] = 4

Use numeric keyed dict instead of list

--It's okay if the key is missing

python


a = {
    1: "apple",
    3: "orange",
}

Use numeric keyed dict instead of list

--Effective when combined with defaultdict

python


from collections import defaultdict
a = defaultdict(str)
a[1] = "apple"
a[3] = "orange"
l = max(l.keys())
for i in range(0, l + 1):
    print("%d: %s" % (i, a[i]))
0: 
1: apple
2: 
3: orange

Recommended Posts

Use something other than a <br> string for the <br> dict key in Python
[Introduction to Python] How to use the in operator in a for statement?
Check if the string is a number in python
use something other than the default profile with boto3
[Python] I want to add a static directory with Flask [I want to use something other than static]
[Python] Use a string sequence
Use list-keyed dict in Python
Find out the apparent width of a string in python
How to use the __call__ method in a Python class
Create a random string in Python
Wrap (part of) the AtCoder Library in Cython for use in Python
python: Use the variables defined in the string format as they are
[Python] How to get a value with a key other than value with Enum
Get the formula in an excel file as a string in Python
A memo for those who use Python in Visual Studio (me)
Use print in a Python2 lambda expression
Write the test in a python docstring
Use Resource API rather than Client API in AWS SDK for Python (Boto3)
Change the list in a for statement
Run the Python interpreter in a script
MongoDB for the first time in Python
Get a token for conoha in python
The eval () function that calculates a string as an expression in python
Generate a class from a string in Python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
A useful note when using Python for the first time in a while
Get the key for the second layer migration of JSON data in python
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
I searched for the skills needed to become a web engineer in Python
Turn multiple lists with a for statement at the same time in Python
How to use the C library in Python
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
[Python] Get the files in a folder with Python
Created a Python wrapper for the Qiita API
How to embed a variable in a python string
Use the latest pip in a virtualenv environment
Get the caller of a function in Python
Use logger with Python for the time being
Make a copy of the list in Python
Tips for hitting the ATND API in Python
Python: Prepare a serializer for the class instance:
Don't use readlines () in your Python for statement!
Sort dict in dict (dictionary in dictionary) with a specific key
Create a datetime object from a string in Python (Python 3.3)
Output in the form of a python array
Use a custom error page in python / tornado
[Python] Find the transposed matrix in a comprehension
json.dumping None in python returns the string null
Use the LibreOffice app in Python (3) Add library
Put out a shortened URL string in Python
Insert an object inside a string in Python
[Windows] The problem that an error occurs when opening a file other than CP932 (Shift-JIS) encoded in Python has been solved for the time being.
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
Automate background removal for the latest portraits in a directory with Python and API
Get the value of a specific key in a list from the dictionary type in the list with Python
A memo when checking whether the specified key exists in the defined dictionary with python
The concept of reference in Python collapsed for a moment, so I experimented a little.
[Python] Leave only the elements that start with a specific character string in the array
[For beginners] How to use say command in python!
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python