2

Hi I have two models named A and B. I want to get the ids from my model B and compare it with the id in model A (Foreign Key relation). I used the following method to get the ids a = B.objects.filter(b_id=object.id).values_list('id')

But a printed a result of [(82L,), (83L,), (84L,), (85L,)] instead of [82, 83 ,] etc. How can I get a flat list without the appended Ls?

Mike Covington
  • 2,147
  • 1
  • 16
  • 26
gentle
  • 151
  • 2
  • 2
  • 14
  • Possible duplicate of [Python/Django: Creating a simpler list from values\_list()](http://stackoverflow.com/questions/699462/python-django-creating-a-simpler-list-from-values-list) – Mp0int Oct 06 '15 at 07:09

2 Answers2

5

values_list() by default returns the values grouped into tuples.

If you don't want that, use

values_list('id', flat=True)
dhke
  • 15,008
  • 2
  • 39
  • 56
  • Yeah, Thanks dhke. But It returned like [82L, 83L, 84L, 85L]. Any idea why? – gentle Oct 06 '15 at 06:54
  • @gentle: http://stackoverflow.com/questions/11764713/why-do-integers-in-database-row-tuple-have-an-l-suffix – Mike Covington Oct 06 '15 at 06:59
  • @gentle Hard to say without knowing your models and your database. `L` indicates this is a *long integer literal*. Are you using MySQL? MySQL's dbapi implementation seems to [return long integers for all integer types](https://stackoverflow.com/questions/7733002/django-integerfield-returning-long). It should make no difference (other than maybe a little performance-wise). – dhke Oct 06 '15 at 07:02
  • the "L" suffixe means "long integer" and is only printed as the _inner representation_ of the `int` object. You don't have to care about it. – bruno desthuilliers Oct 06 '15 at 08:29
2

These numbers are long integers. As such, they are returned with an L suffix. There really is no letter 'L' after the number though. This has been removed in Python 3, since all integers are now treated as long integers. I don't believe what you are seeing is any cause for alarm.

Edit: See why-do-integers-in-database-row-tuple-have-an-l-suffix for more info.

Community
  • 1
  • 1
Mike Covington
  • 2,147
  • 1
  • 16
  • 26
  • Hi mfcovington, Thanks but am using python 2.7. That's why the concern – gentle Oct 06 '15 at 07:00
  • @gentle Ya, I figured you must be using python 2, since you are seeing it. Is it actually causing problems or just bothering you? If you want to display it for some reason and not have the `L` included, you can use `str()` which should return just the digits. – Mike Covington Oct 06 '15 at 07:03
  • Because later I need to compare these ids with real int ids from other models. That's why this concern – gentle Oct 06 '15 at 07:15
  • @gentle: But they are real integers, a 'long' integer can just be larger than a 'plain' integers. You can compare them just fine to one another. See: https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex – Mike Covington Oct 06 '15 at 07:18
  • Oh I see. Thanks a lot mfcovington :) – gentle Oct 06 '15 at 07:21
  • No problem! By the way, try this: `x = long(5); y = 5; type(x); type(y); x == y` – Mike Covington Oct 06 '15 at 07:21