Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

square brackets after a function call

Tags:

python

I am a total novice at Python, and have come across a piece of code that confuses me.

ts, pkt2 = capPort2.wait(1, 45)[0]

The previous line confuses me. I understand the call to the function wait with the two parameters, but what does the [0] mean or do?

like image 658
JohnB Avatar asked Jun 21 '11 08:06

JohnB


1 Answers

It means to extract the first item in the list/tuple return by the function.

In [1]: "this is a long sentence".split()
Out[1]: ['this', 'is', 'a', 'long', 'sentence']

In [2]: "this is a long sentence".split()[0]
Out[2]: 'this'
like image 55
Fredrik Pihl Avatar answered Sep 25 '22 22:09

Fredrik Pihl