Reputation: 197
f.e. we have a list with 1 element
i = ['x']
i - the list always with 1 element
what way is more pythonic or performance:
element = i[0]
or
element = i.pop()
We dont care about that list, so if we "cut" element with pop - it is no problem for us
Upvotes: 4
Views: 1381
Reputation: 6332
I believe the most pythonic would be:
element, = i
That statement is equal to element = i[0]
, but has the benefit of being shorter and producing an error if the array is not of size 1.
Upvotes: 0
Reputation: 1153
i[0]
is more performant, since it has fewer operations to carry out.
If there's no reason to pop from the list, then why do it? That's just confusing to the next programmer looking at your code. Use i[0]
.
Upvotes: 3