Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Set Speed Test

Minh Pham on March 25, 2020

Hi James,

Please tell us how to set up the Python Console and test as you do in this Speed Test

Thanks Minh

James Uejio RP Team on April 4, 2020

Hi @Minh thank you for the response. See ipython.org/install.html for how to install iPython where you can use timeit.

Zarata on April 6, 2020

The tuple tutorial made a point that tuples are used because they are faster for many operations compared to lists. You’ve proven that the membership operation is “the same” tuple vs. list. Is this detail worth an explanation / rectification?

keyurratanghayra on April 7, 2020

Hey There,

May I know which IDE, you are using for this lesson? It looks swanky.

Ricky White RP Team on April 8, 2020

He’s using VS Code :)

James Uejio RP Team on April 11, 2020

@keyurratanghayra Ricky is correct I am using VS Code! I have a “Dainty – Monokai” theme see dainty.site/vs. Also using iTerm for my terminal.

@Zarata I’m a little confused regarding the question. At around 5:25 you can see membership in sets is 57 microseconds while membership in lists is 5 milliseconds. So membership in sets is 100x faster. Let me know if you are referring to another part of the video!

Zarata on April 13, 2020

In the Introduction to Python learning path as a whole, there is another module Lists and Tuples. In that module (not this Sets module) the author made the point that tuples are used because they are faster than lists. Your video clocks comparable list and tuple operations as ~5.7 ms both. Your video validates the speed superiority of set implementation, but invalidates that tuple/list statement by the other author. I’m curious if there’s a rectification. Your timing example re-ignites the question I remember from the tuples/list module “why use tuples at all?”

BTW, I recall someone along the way asked “what are sets good for?” I have found set concepts VERY useful in some graph theory numerical analysis I performed using Java, but I didn’t find in Java at the time all of the method and operator support that Python provides. To that person: sets do have real world use and the set concept can make some numerical analysis problems “easier”.

Awesome, didn’t know about the %timeit functionality of ipython. Is that why you’re using ipython instead of ptpython? Or are there any other advantages?

James Uejio RP Team on April 15, 2020

@zarata Great observation and after some research, the article is correct to an extent. Tuples are only faster than lists when initializing them, not necessarily adding them together (like in the video). For example:

>>> %timeit (1, 2, 3)
6.9 ns ± 0.0379 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
>>> %timeit [1, 2, 3]
46.4 ns ± 0.341 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In terms of what I showed in the video, you can see lists are slightly faster because for tuples you need to initialize and add together vs just appending to a list. I probably should have named the functions initalize_and_add or something. In the real world however this is pretty negligible and the it’s not necessarily worth worrying about optimizing tuples vs. lists in terms of speed. You can learn more here: stackoverflow.com/questions/3340539/why-is-tuple-faster-than-list-in-python.

However there are many benefits to tuples because they are hashable so you can use them as keys for dictionaries or you can put them in sets. They are also immutable which can lead to cleaner code and thread safe programming. See hackernoon.com/5-benefits-of-immutable-objects-worth-considering-for-your-next-project-f98e7e85b6ac for more!

@RMS I’m not super familiar with pypython but ipython has been pretty easy for me to use. I don’t think the REPL matters too much (sometimes when I’m lazy I’ll just pull up the built in one to double check some syntax), but ipython is easy to install and use so I chose that one. This video course realpython.com/courses/finding-perfect-python-code-editor/ has some more information on choosing a python code editor, which is probably more useful than finding the perfect REPL.

Become a Member to join the conversation.