Snippets tagged “dictionary”
25 snippets use this tag.
- Add a new item to a dictionary in PythonPython
To add a new item (key-value pair) to a dictionary in Python, you can use the square brackets [] or the update() method.
- Alternatives for returning multiple values from a Python functionPython
Using a tuple:
- Check if a given key already exists in a dictionaryPython
To check if a given key already exists in a dictionary, you can use the in keyword.
- Convert a String representation of a Dictionary to a dictionaryPython
You can use the json module in Python to convert a string representation of a dictionary to a dictionary.
- Convert Python dict into a dataframePython
You can use the Pandas library to convert a Python dictionary into a dataframe.
- Converting Dictionary to List?Python
In Python, you can convert a dictionary to a list of its keys or values using the keys() and values() methods, respectively.
- Delete an element from a dictionaryPython
To delete an element from a dictionary in Python, you can use the del statement.
- Get key by value in dictionaryPython
You can use the in keyword to check if a value exists in a dictionary, and then use the items() method to return a list of key-value pairs.
- Getting key with maximum value in dictionary?Python
You can use the built-in max() function in Python to get the key with the maximum value in a dictionary.
- How can I add new keys to a dictionary?Python
To add a new key-value pair to a dictionary in Python, you can use the update() method or simply assign a value to a new key using square brackets [].
- How can I get list of values from dict?Python
You can use the values() method to get a list of values from a dictionary:
- How can I make a dictionary (dict) from separate lists of keys and values?Python
You can use the zip function to create a dictionary from separate lists of keys and values like this:
- How can I remove a key from a Python dictionary?Python
You can use the del statement to remove a key-value pair from a dictionary in Python.
- How do I merge two dictionaries in a single expression?Python
You can use the update() method of one dictionary to merge the key-value pairs from another dictionary into it.
- How do I print the key-value pairs of a dictionary in pythonPython
You can use the items() method to print the key-value pairs of a dictionary in Python.
- How do I sort a dictionary by key?Python
In Python, dictionaries are unordered collections of key-value pairs.
- How do I sort a dictionary by value?Python
You can use the sorted() function and pass it the dictionary, along with the key parameter set to a lambda function that returns the value from the dictionary.
- How to copy a dictionary and only edit the copyPython
There are a few ways to copy a dictionary in Python, depending on the level of copying that you need.
- How to print a dictionary's key?Python
To print the keys of a dictionary in Python, you can use the built-in keys() method.
- I'm getting Key error in pythonPython
A KeyError in Python is raised when a dictionary key is not found in the dictionary.
- Iterating over dictionaries using 'for' loopsPython
To iterate over a dictionary using a for loop, you can use the .items() method to get a list of the dictionary's keys and values, and then iterate over that list.
- Renaming column names in PandasPython
To rename the column names of a Pandas DataFrame, you can use the DataFrame.rename() method.
- Reverse / invert a dictionary mappingPython
Here's an example of how you can reverse or invert a dictionary mapping in Python:
- What is the difference between dict.items() and dict.iteritems() in Python2?Python
In Python 2, dict.items() returns a list of the dictionary's key-value pairs, whereas dict.iteritems() returns an iterator over the dictionary's key-value pairs.
- What is the purpose and use of **kwargs?Python
In Python, kwargs is a way to pass a keyworded, variable-length argument list.