Building a dict out of another dict by using a for loop

To go through a dict while building a second one, use a for loop and assign each pair in the old dict to the new dict, like this:

>>> mydict = {'bed price': '800 pounds', 'bed length': '2 foot long'}

TECHNICAL STUFF

for key, value in mydict.items():

In Python 2.1 and earlier, the in keyword doesn't work with a dictionary object; you need to use it with a dictionary method. So to write the above loop, you could use this syntax:

Was this article helpful?

0 0

Post a comment