0

I have seen many posts here, which gives ways of removing the trailing L from a list of python long integers. The most proposed way is

print map(int,list)

However this seems not to work always.

Example---

A=[4198400644L, 3764083286L, 2895448686L, 1158179486, 2316359001L]
print map(int,A)

The above code gives the same result as the input. I have noticed that the map method doesn't work whenever the number preceding L is large, and only when the numbers are in a list. e.g. Application of int() on 4198400644L does give the number without L, when out of the list.

Why is this occurring and more importantly, how to overcome this?

I think I really need to remove this L, because this is a small part of a program where I need to multiply some integer from this list A, with some integer from a list of non-long integers, and this L is disturbing.I could ofcourse convert the long integers into string,remove the L and convert them back to integer.But is there another way?

I am still using the now outdated Python 2.7.

Manas Dogra
  • 317
  • 1
  • 15
  • 1
    Why does the `L` suffix concern you? Can you not just do the multiplication... and then whenever you `print` or output it as a string then you won't see the `L` anyway... you're only seeing it because you're seeing the `repr` of the object – Jon Clements Aug 15 '20 at 14:09
  • 1
    Please just use Python3. Splitting integers into `int` and `long` is an implementation of Python2 detail that you should not bother with. Large integers are always `long`, you are merely looking at the representation wrongly - containers force `repr` on their elements, print uses `str` for bare values instead. – MisterMiyagi Aug 15 '20 at 14:23
  • Compare ``print sys.maxsize + 1``, ``print int(sys.maxsize + 1)``, ``print repr(int(sys.maxsize + 1))``. Note that it looks like you are using a 32-bit build of Python – on a 64-bit build ``int`` ranges from ``-9223372036854775808`` to ``9223372036854775807``. – MisterMiyagi Aug 15 '20 at 14:32

1 Answers1

1

Python has two different kinds of integers. The int type is used for those that fit into 32 bits, or -0x80000000 to 0x7fffffff. The long type is for anything outside that range, as all your examples are. The difference is marked with the L appended to the number, but only when you use repr(n) as is done automatically when the number is part of a list.

In Python 3 they realized that this difference was arbitrary and unnecessary. Any int can be as large as you want, and long is no longer a type. You won't see repr put the trailing L on any numbers no matter how large, and adding it yourself on a constant is a syntax error.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622