I have a dict with following structure:
{5:"djdj", 6:"8899", 7:"998kdj"}
The key is int typed and it's not sorted.
Now I want all the elements whose key is >= 6.
Is there easy way to do that?
The compare method cmp() is used in Python to compare values and keys of two dictionaries. If method returns 0 if both dictionaries are equal, 1 if dic1 > dict2 and -1 if dict1 < dict2.
First, a given key can appear in a dictionary only once. Duplicate keys are not allowed.
Filter a Dictionary by filter() Instead of creating our own function we can also use python's filter() function too. filter() function accepts a, an iterable sequence to be filtered. a function that accepts an argument and returns bool i.e. True or False based on it's logic.
The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.
[v for k,v in mydict.items() if k >= 6]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With