0

I am new to python and I have seen this several times:

>>> nums = [1,2,3,4,5]
>>> [(x,y) for x in nums for y in nums]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
>>> min(_, key=lambda pair: pair[0]/pair[1])
(1, 5)

In the code above what is the purpose of: the "_" in the min function and where else can it be used?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Nick
  • 159
  • 1
  • 7
  • http://stackoverflow.com/questions/5787277/python-underscore-as-a-function-parameter – simchona Apr 19 '12 at 16:22
  • Possible duplicate of [Python underscore as a function parameter](http://stackoverflow.com/questions/5787277/python-underscore-as-a-function-parameter) – ShadowRanger Apr 19 '17 at 00:26

1 Answers1

4

In the interactive interpreter _ is used to reference the last returned value.

Eg

>>> 2 + 4
6
>>> _ + 4
10

So you can use it as an argument in a function as well

>>> 2 + 4
6
>>> for i in range(_): print(i)
0
1
2
3
4
5
LexyStardust
  • 1,018
  • 5
  • 18