Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to to filter dict to select only keys greater than a value? [duplicate]

Tags:

python

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?

like image 947
Bin Chen Avatar asked Nov 15 '10 04:11

Bin Chen


People also ask

How do you compare keys in a dictionary?

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.

Does dictionary accept duplicate keys?

First, a given key can appear in a dictionary only once. Duplicate keys are not allowed.

How do you filter data in a dictionary Python?

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.

Can dictionary contains duplicate values?

The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.


1 Answers

[v for k,v in mydict.items() if k >= 6]

like image 188
jonesy Avatar answered Sep 28 '22 12:09

jonesy