1

I have a dictionary, and I want to iterate over the keys, test the keys, and then print the keys that pass the test.

For example, my code looks like this at the moment:

x = {4:"a", 1:"b", 0:"c", 9:"d"} for t in x: if t > 2: print t 

However, all the output is, is just the key.

How do I get the code to print out the value at the end of the code, instead of the key?

Also, how do I get this in the form of a dictionary, {key : value}?

9 Answers 9

5

You could try this: print t, x[t]

Or this:

for key, val in x.items(): if key > 2: print key, val 

If you want to get only the pairs that you're printing into a new dictionary, then you have to put them there:

newdict = {} for key, val in x.items(): if key > 2: print key, val # put all the keys > 2 into the new dict newdict[key] = val 

Of course that solution is printing when it inserts. If you're doing more than just a little script you will get more utility out of breaking functionality out, making sure that any given function does one, and only one, thing:

def filter_dict(x): newdict = {} for key, val in x.items(): if key > 2: # put all the keys > 2 into the new dict newdict[key] = val def pretty_print(dct): for k, v in dct.items(): print k, v filtered = filter_dict(x) pretty_print(dct) 

This doesn't apply if you're just debugging, of course, and there are ways in which it depends on how large of a program you're writing: if you're doing a simple enough thing then the extra flexibility you get by breaking functionality up is lost because of the extra effort of figuring out exactly what any given thing does. That said: you should generally do it.

Additionally, the most idiomatic way to filter lists on a simple comparison in Python is to use a dict comprehension (new in Python 2.7/3.0):

filtered = {key: val for key, val in x if key > 2} 
Sign up to request clarification or add additional context in comments.

1 Comment

The SO way to say thanks is to up-vote and/or accept :-) Also you're welcome.
2
print [(k,v) for k,v in yourDict.items() if test(k)] 

(You could just do k for k,v in... or v for k,v in.... If you only need values, you can use yourDict.values().)

Comments

1
x = {4:"a", 1:"b", 0:"c", 9:"d"} for t in x: if t > 2: print '{}: {}'.format(t, x[t]) 

Slightly more pythonic:

>>> for key, value in x.iteritems(): ... if key > 2: ... print '{}: {}'.format(key, value) ... 4: a 9: d 

edit: To just print the value:

>>> for key, value in x.iteritems(): ... if key > 2: ... print value ... a d 

1 Comment

what if i just want it to print out the value? like, just the last term in the pair? thannk you!
1

key as well as value:

x = {4:"a", 1:"b", 0:"c", 9:"d"} for k,v in x.items(): if k>2: print"{%d : %s}"%(k,repr(v)) {4 : 'a'} {9 : 'd'} 

just value:

x = {4:"a", 1:"b", 0:"c", 9:"d"} for k,v in x.items(): if k>2:print v a d 

Comments

1

Just the keys:

>>> [k for k in x.iterkeys() if k > 2] [4, 9] 

Just the values:

>>> [v for k,v in x.iteritems() if k > 2] ['a', 'd'] 

Key-value pairs:

>>> [(k, v) for k,v in x.iteritems() if k > 2] [(4, 'a'), (9, 'd')] 

As a dict:

>>> dict((k,v) for k,v in x.iteritems() if k > 2) {9: 'd', 4: 'a'} 

Comments

1

If you want to create a dictionary with subset of items, you can also use a dictionary comprehension for Python 2.7+:

>>> x = {4:"a", 1:"b", 0:"c", 9:"d"} >>> y = {k:v for k, v in x.iteritems() if k > 2} >>> y {9: 'd', 4: 'a'} 

This is only a newer equivalent with only minor syntax differences for what Johnysweb shows in his answer:

>>> y = dict((k, v) for k, v in x.iteritems() if k > 2) >>> y {9: 'd', 4: 'a'} 

1 Comment

Or even just {k:x[k] for k in x if k > 2} which is probably the cleanest of all (assuming python 2.7+)
1

The following list comprehension will print the key and value if key > 2.

Try this:

print [(key, value) for key, value in x.iteritems() if key > 2] 

Comments

0

This solution prints the key t and the value in the dictionary x at key t.

x = {4:"a", 1:"b", 0:"c", 9:"d"} for t in x: if t > 2: print t, x[t] 

Comments

0

Using lambda function

(lambda x:[(k,v) for k,v in x.iteritems() if k > 2])(x)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.