Call list()
on the dictionary instead:
keys = list(test)
In Python 3, the dict.keys()
method returns a dictionary view object, which acts as a set. Iterating over the dictionary directly also yields keys, so turning a dictionary into a list results in a list of all the keys:
>>> test = {'foo': 'bar', 'hello': 'world'} >>> list(test) ['foo', 'hello'] >>> list(test)[0] 'foo'
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’ “
- How can I get dictionary key as variable directly in Python (not by searching from value)?
- Are dictionaries ordered in Python 3.6+?
- How to add multiple values per key in python dictionary
- How to add multiple values to a dictionary key in python?
- enumerate() for dictionary in python
- Creating 2D dictionary in Python
- How to print a dictionary’s key?
- 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?
- Iterating over dictionary items(), values(), keys() in Python 3
- How to open a file using the open with statement
- How do I sort a dictionary by value?
- Relative imports – ModuleNotFoundError: No module named x
- Best way to convert string to bytes in Python 3?
- PermissionError: [Errno 13] Permission denied
- What is setup.py?
- How to remove EOFError: EOF when reading a line?
- TypeError: cannot unpack non-iterable NoneType object
- Is there a ‘foreach’ function in Python 3?
- Python inline if statement
- How to print like printf in Python3?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Could not find a version that satisfies the requirement tensorflow
- How can I remove a key from a Python dictionary?
- How to solve dictionary changed size during iteration error?
- DataFrame constructor not properly called
- Invalid character in identifier
- Getting key with maximum value in dictionary?
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- How to find all the indexes of a recurring item in a list?
- pandas: merge (join) two data frames on multiple columns
- How do you use subprocess.check_output() in Python?
- (Help) TypeError: ‘str’ object cannot be interpreted as an integer
- Why KeyError: 0
- builtins.TypeError: must be str, not bytes
- TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 )
- takes 1 positional argument but 2 were given
- Python 3 ImportError: No module named ‘ConfigParser’
- Why am I getting this syntax error: keyword can’t be an expression
- re.search().TypeError: cannot use a string pattern on a bytes-like object
- Add Legend to Seaborn point plot
- ctypes – Beginner
- Get key by value in dictionary
- How can I install Python’s pip3 on my Mac?
- Get ZeroDivisionError: float division in python
- TypeError: ‘encoding’ is an invalid keyword argument for this function
- What is the Python 3 equivalent of “python -m SimpleHTTPServer”
- What does -> mean in Python function definitions?
- ‘dict’ object has no attribute ‘has_key’
- Convert a Pandas DataFrame to a dictionary
- In Python, when to use a Dictionary, List or Set?
- 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
- How to troubleshoot an “AttributeError: __exit__” in multiproccesing in Python?
- TypeError: cannot unpack non-iterable NoneType object
- How can I create an array/list of dictionaries in python?
- Shebang doesn’t work with python3
- Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings
- Python map object is not subscriptable
- pythonw.exe or python.exe?
- .pyw files in python program
- How to convert an XML string to a dictionary?
- 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
- 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?
- Get a random sample with replacement
- How to convert a file into a dictionary?
- Conda command is not recognized on Windows 10
- Append a dictionary to a dictionary
- ImportError: No module named ‘django.core.urlresolvers’
- Python 3: Multiply a vector by a matrix without NumPy
- How to fix “Can’t find a default Python” error
- TypeError: ‘newline’ is an invalid keyword argument for this function
- Print empty line?
- JSON object must be str, bytes or bytearray, not dict
- Checking to see if a string is an integer or float
- How to know/change current directory in Python shell?
- Change the name of a key in dictionary
- How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?
- How to use cmp() in Python 3?
- What is the correct format to write float value to file in Python
- “E: Unable to locate package python3-pip”
- Error: No module named ‘fcntl’
- Python can’t find file
- Create a list with initial capacity in Python
- How to install PyQt4 on Windows using pip?
- KeyError: ‘plotly_domain’
- TypeError: ‘zip’ object is not subscriptable