Ben
Ben

Reputation: 2431

In Python, how can I print a dictionary containing large numbers without an 'L' being inserted after the large numbers?

I have a dictionary as follows

d={'apples':1349532000000, 'pears':1349532000000}

Doing either str(d) or repr(d) results in the following output

{'apples': 1349532000000L, 'pears': 1349532000000L}

How can I get str, repr, or print to display the dictionary without it adding an L to the numbers?

I am using Python 2.7

Upvotes: 1

Views: 375

Answers (2)

Ben
Ben

Reputation: 2431

Well this is a little embarrasing, I just found a solution after quite a few attempts :-P

One way to do this (which suits my purpose) is to use json.dumps() to convert the dictionary to a string.

d={'apples':1349532000000, 'pears':1349532000000}

import json
json.dumps(d)

Outputs

'{"apples": 1349532000000, "pears": 1349532000000}'

Upvotes: 0

cdhowie
cdhowie

Reputation: 168988

You can't, because the L suffix denotes a 64-bit integer. Without it, those numbers are 32-bit integers. Those numbers don't fit into 32 bits because they are too large. If the L suffix was omitted, the result would not be valid Python, and the whole point of repr() is to emit valid Python.

Upvotes: 1

Related Questions