0

I have a list that only has one item. I know this list only has one item because it is the output of a MySQL query that will always have just one result.

MySQL Query:

SELECT MAX(date) FROM db.table;

List result:

['20170307']

Now I must access the item in this list. I have two options:

1) By position

max_date = list[0]
print(max_date)

2) By iterating through item(s)

for item in list:
    max_date = item
print(max_date)

Can someone please explain which approach is more appropriate in terms performance, readability, and overall best practice? My IDE raises a red flag when I use approach #2 but is it safe to ignore if I'm confident my list will always have one item?

ProgrammingWithRandy
  • 594
  • 2
  • 10
  • 28
  • 1
    This kind of question is likely to attract opinion-based answers. That said, nothing wrong with `somelist[0]`. – Paulo Scardine Mar 29 '17 at 18:52
  • Check length for errors, but use the index position – ryugie Mar 29 '17 at 18:53
  • Or even using tuple unpacking: `max_date, = somelist`, – Daniel Roseman Mar 29 '17 at 18:53
  • This is an opinion question and should be closed. Personally I prefer `somelist[len([x for x in ""])]` – xandermonkey Mar 29 '17 at 18:53
  • Why are you asking for the proper way to *iterate through* the list? Why did you think of this in terms of iteration and pick an initial implementation in terms of iteration? – user2357112 supports Monica Mar 29 '17 at 18:54
  • Second is a better way as you might get 0 result from database and in that case 1st method will give an error. IDE gives error flag as if there are no results from db, `max_date` will not be set and the print will give an error. To solve that, before iteration set the `max_date` to None: `max_date = None`, it will solve the flag – Emin Mastizada Mar 29 '17 at 18:55

4 Answers4

3

I'd use a third option, sequence-unpacking syntax:

max_date, = list

Note the comma. This has the benefit of raising an exception if list is not in fact a sequence of length 1.

In terms of speed, list[0] almost certainly runs fastest.

Tim Peters
  • 55,793
  • 10
  • 105
  • 118
  • 1
    Actually, at least on CPython, sequence unpacking is faster (tested on Py2.7.12 and 3.5.2); indexing involves loading both list and index, then executing a subscript operation, which, even though there is a fast path for `list` on `BINARY_SUBSCR`, still runs slower than a single `UNPACK_SEQUENCE` instruction. For non-`list` sequences without the subscript fast path, unpacking wins by even more. – ShadowRanger Apr 07 '17 at 14:02
0

Ideally, using the by position style works since all you need is the first element of the list. Just by reading the code, the for loop gives me an impression of more than 1 element in the list

Anuparna
  • 98
  • 1
  • 9
0

"list" is a reserved keyword. DO NOT use it as a variable name, it will lead to erractic behavior.If you really want to be safe, you can do:

if len(list1) == 1:
    max_item = list1[0]
print(max_item)
Mikael
  • 387
  • 3
  • 20
  • To be clear, `list` is neither reserved, nor a keyword (if it was, you *couldn't* use it as a variable name). You *shouldn't* use it as a variable name, because doing so shadows the `list` constructor, removing your ability to use it in the current (and more deeply nested) scopes. – ShadowRanger Aug 01 '19 at 00:35
0

Access by index is very fast is most languages. You can also use the automatic unpacking in Python:

try:
    first, = list_with_len_1
except ValueError:
    first = None
Paulo Scardine
  • 60,096
  • 9
  • 116
  • 138