beetroot
beetroot

Reputation: 4519

Formatting dictionary entries

I'm running python 2.7 in PS on a w10. I want to print the key and the value of a dictionary with every pair enumerated.

I do the following:

my_dict = {'key_one': 1, 'key_two': 2, 'key_three': 3}

for k, v in enumerate(my_dict.iteritems(), start = 1):
     print k, v

which in turn gives: 1 ('key_one', 1) 2 ('key_two', 2) 3 ('key_three', 3)

How do I return the entries without the braces?

Example - I want to put a = sign in between my key-value pairs.

Upvotes: 0

Views: 386

Answers (6)

user3030010
user3030010

Reputation: 1857

If you want to keep the indicies (from enumerate), then you're going to have to unpack the key and value from the dict items separates. Right now what you're calling k is actually an index, and what you're calling v is actually a key-value pair. Try something like this:

for i, (k, v) in enumerate(my_dict.iteritems(), start=1):
    print i, k, v

That results in something like:

1 key_two 2
2 key_one 1
3 key_three 3

To get them formatted with an equals sign, you'd have to change the print statement to print i, "{}={}".format(k, v), which would result in something like:

1 key_two=2
2 key_one=1
3 key_three=3

If you need to retrieve the keys in a consistent order, use sorted(), like this:

for i, (k, v) in enumerate(sorted(my_dict.iteritems()), start=1):
    ...

Or, if you want to sort by values first instead of the keys first, you could specify a key function for the sorted() call. That would look like: sorted(my_dict.iteritems(), key=lambda (x, y): (y, x)). That would give you an output of

1 key_one=1
2 key_two=2
3 key_three=3

Upvotes: 2

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48057

You do not need enumerate() here. It is used when you need to iterate along with the index. you do not even need str.format() for achieving this. Simply place a entry of '=' string between your key, value and you will get what your desire. For example:

>>> my_dict = {'key_one': 1, 'key_two': 2, 'key_three': 3}
>>> for key, value in my_dict.items():
...     print key, '=', value
... 
key_two = 2
key_one = 1
key_three = 3

Edit: Based on the comment at user3030010's answer

Note: dict in python are un ordered. In case you want to maintain the order, use collections.OrderedDict() instead. It will preserve the order independent of the platform and python version. For example if you created the dict like:

>>> from collections import OrderedDict
>>> my_dict = OrderedDict()
>>> my_dict['key_one'] = 1
>>> my_dict['key_two'] = 2
>>> my_dict['key_three'] = 3

On iterating it, you will always get the same response as:

>>> for key, value in my_dict.items():
...     print key, '=', value
... 
key_one = 1
key_two = 2
key_three = 3

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Simple one-line solution(for Python 2.7):

print '\n'.join([k+'='+ str(my_dict[k]) for k in my_dict.keys()])

The output:

key_two=2                                                                                                                                                                                                                                              
key_one=1                                                                                                                                                                                                                                              
key_three=3

Upvotes: 0

shaktimaan
shaktimaan

Reputation: 12092

You don't need enumerate if you just want to print the existing key and values in your dictionary. Just use format():

for k, v in my_dict.items():
    print '{} = {}'.format(k, v)

This would give:

key_one = 1
key_two = 2
key_three = 3

Upvotes: 1

Paul Theis AneoPsy
Paul Theis AneoPsy

Reputation: 21

Like this?

>>> for k, v in my_dict.iteritems():
...      print k, v
... 
key_two 2
key_one 1
key_three 3

or

>>> for i, (k, v) in enumerate(my_dict.iteritems(), start=1):
...     print i, k, v
... 
1 key_two 2
2 key_one 1
3 key_three 3

Upvotes: 0

Vivek Srinivasan
Vivek Srinivasan

Reputation: 2887

This works

my_dict = {'key_one': 1, 'key_two': 2, 'key_three': 3}
for key,value in my_dict.iteritems():
    print key,value

Upvotes: 0

Related Questions