2

I have following code

li = [[[1,2],[3,4]]]
min([*x[0] for x in li])

I expected to see

Output: 1

But what happened is

SyntaxError: iterable unpacking cannot be used in comprehension

What is the problem?

iwbtrs
  • 358
  • 4
  • 13
  • 1
    you can only use unpacking (the `*` notation in function calls) – Nullman Jan 26 '20 at 15:05
  • Always share the entire error message. Speaking of, what don't you understand from that? – AMC Jan 26 '20 at 21:27
  • Does this answer your question? [Unpacking tuples in a python list comprehension (cannot use the \*-operator)](https://stackoverflow.com/questions/37279653/unpacking-tuples-in-a-python-list-comprehension-cannot-use-the-operator) – AMC Jan 26 '20 at 21:27

2 Answers2

1

Or Just point the first element:

li = [[[1,2],[3,4]]]
min([x[0] for x in li[0]])

Or iterate all the canonized lists:

min([x[0] for xs in li for x in xs])
Green
  • 2,405
  • 3
  • 22
  • 46
0

Probably, you are looking for something like this:

li = [[[1,2],[3,4]]]
min(li[0], key=lambda x: x[0])

If you would like to receive a pair with the minimum first argument, just provide your own comparison function to min as key.

Yann
  • 2,426
  • 1
  • 16
  • 33