+ 1

Which is faster?

x = [1,2,3] x.pop() or x = (1,2,3) x = x[:2]

9th Aug 2020, 9:48 AM
Hello World
Hello World - avatar
4 Réponses
0
We can do a simple experiment on this. https://code.sololearn.com/cchelRgdWPo4/?ref=app Averagely speaking, it seems that your second code runs faster than the first one, but they both run very fast (each takes less than 1 ms), so you can choose the code you like, and get similar performance.
9th Aug 2020, 3:19 PM
Panko
Panko - avatar
+ 4
Why you comparing for the fastness. Both perform different operations.. In first, you removing last element jus from a list. In 2nd, you are copying 0 to last element into a variable from a tuple. It is more operations than 1st. Since there is just 3 elements no difference. But I think not comparable for fastness..
9th Aug 2020, 10:39 AM
Jayakrishna 🇮🇳
+ 1
Thanks
9th Aug 2020, 10:42 AM
Hello World
Hello World - avatar
+ 1
Hello World Good thinking.. I think those are 2 different operations but checked with time differences the pop operation taking more nano milliseconds than 2nd way slicing... Hope this helps... & You're welcome...
9th Aug 2020, 3:30 PM
Jayakrishna 🇮🇳