On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary.
The normal case you enumerate the keys of the dictionary:
example_dict = {1:'a', 2:'b', 3:'c', 4:'d'} for i, k in enumerate(example_dict): print(i, k)
Which outputs:
0 1 1 2 2 3 3 4
But if you want to enumerate through both keys and values this is the way:
for i, (k, v) in enumerate(example_dict.items()): print(i, k, v)
Which outputs:
0 1 a 1 2 b 2 3 c 3 4 d
Related Posts:
- Updating a dictionary in python
- How to copy a dictionary and only edit the copy
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- Are dictionaries ordered in Python 3.6+?
- How do I format a string using a dictionary in python-3.x?
- How to return dictionary keys as a list in Python?
- How to save a dictionary to a file?
- TypeError: ‘dict_keys’ object does not support indexing
- What is an easy way to implement fprintf in python?
- Accessing dict_keys element by index in Python3
- Iterating over dictionary items(), values(), keys() in Python 3
- Use Gif Logo For Loading Screen In Kivy
- How can I make a time delay in Python? [duplicate]
- Python – TypeError: ‘int’ object is not iterable
- What is the difference between rw+ and r+
- Python – TypeError: ‘int’ object is not iterable
- Python: ‘ModuleNotFoundError’ when trying to import module from imported package
- Check if a given key already exists in a dictionary
- What does the list() function do in Python?
- Is a Python dictionary an example of a hash table?
- Converting dictionary to JSON
- Invalid character in identifier
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Convert bytes to a string
- Python Error: AttributeError: __enter__ [duplicate]
- filedialog, tkinter and opening files
- TypeError: ‘dict’ object is not callable
- Error in Python script “Expected 2D array, got 1D array instead:”?
- Name ‘xrange’ is not defined in Python 3 [duplicate]
- (Help) TypeError: ‘str’ object cannot be interpreted as an integer
- How do order of operations go on Python?
- How to upgrade pip3?
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- TypeError: cannot convert the series to
- Pinging servers in Python
- append multiple values for one key in a dictionary [duplicate]
- Import error: No module name urllib2
- Convert a python dict to a string and back
- ‘End of statement expected’ in pycharm
- Delete an element from a dictionary
- Change figure size and figure format in matplotlib
- How can I concatenate str and int objects?
- How to add multiple values per key in python dictionary
- error: Failed to load the native TensorFlow runtime
- ImportError: No module named ‘encodings’
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- Reverse / invert a dictionary mapping
- TypeError: ‘encoding’ is an invalid keyword argument for this function
- How to print a dictionary’s key?
- Convert a Pandas DataFrame to a dictionary
- How to delete last item in list?
- Don’t understand this SyntaxError: illegal target for annotation
- Using Queue in python
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
- SyntaxError invalid token
- For loop and ‘numpy.float64’ object is not iterable error
- Creating a dictionary from a csv file?
- Map to List error: Series object not callable
- TypeError: cannot unpack non-iterable NoneType object
- python mpl_toolkits installation issue
- How can I create an array/list of dictionaries in python?
- Shebang doesn’t work with python3
- How to strip all whitespace from string
- Python Binomial Coefficient
- pythonw.exe or python.exe?
- How to convert an XML string to a dictionary?
- How to convert ‘binary string’ to normal string in Python3?
- python3 TypeError: ‘function’ object is not iterable
- How can I convert a .py to .exe for Python?
- How do I make a dictionary with multiple keys to one value?
- Directing print output to a .txt file
- How to start from second index for for-loop
- Python: How to check if keys exists and retrieve value from Dictionary in descending priority
- Converting a sentence to piglatin in Python
- Writing a dictionary to a csv file with one line for every ‘key: value’
- Python Error – TypeError: input expected at most 1 arguments, got 3 [duplicate]
- A general tree implementation?
- How to convert a file into a dictionary?
- ModuleNotFoundError: No module named ‘seaborn’ in Python IDE
- Is there a ceiling equivalent of // operator in Python?
- TypeError: ‘set’ object does not support indexing
- Root mean square of a function in python
- Print empty line?
- JSON object must be str, bytes or bytearray, not dict
- Checking to see if a string is an integer or float
- pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding’ when trying to install new packages
- How to know/change current directory in Python shell?
- How to use cmp() in Python 3?
- Generator expression must be parenthesized if not sole argument
- Does python have header files like C/C++?
- Error: No module named ‘fcntl’
- Python can’t find file
- TypeError: got multiple values for argument
- IOError: [Errno 32] Broken pipe when piping: `prog.py | othercmd`
- Python webbrowser.open() to open Chrome browser
- Flatten nested dictionaries, compressing keys
- How to print both strings in a dictionary in Python