Python 3.x list comprehension VS tuple generator

2024/11/18 9:41:20

Is there any reason for memory, speed or whatever, that I would want to use:

tuple(i for i in range(5000))

instead of:

[i for i in range(5000)]

If I didn't mind the immutability of tuples

Answer

Basically a list comprehension is faster than a generator expression and as the reason is that its iteration performs in C (Read the @Veedrac's comment for the reason). But the only reason that should use a generator expression within tuple is that you want to perform some operations on your items and/or filter them and more importantly you want a tuple (because of the immutability and its benefits against mutable objects).

After all you can always timeit your code:

In [10]: %timeit tuple(i for i in range(5000))
1000 loops, best of 3: 325 µs per loopIn [11]: %timeit [i for i in range(5000)]
1000 loops, best of 3: 199 µs per loop

Also note that as I mentioned, if you want to use comprehensions you must needed to perform an operation on your items otherwise you can call the function directly on your iterator, which is faster:

In [12]: %timeit list(range(5000))
10000 loops, best of 3: 98.3 µs per loop
https://en.xdnf.cn/q/71073.html

Related Q&A

Using Sphinx with a distutils-built C extension

I have written a Python module including a submodule written in C: the module itself is called foo and the C part is foo._bar. The structure looks like:src/ foo/__init__.py <- contains the public …

PyYAML error: Could not determine a constructor for the tag !vault

I am trying to read a YAML file that has the tag !vault in it. I get the error:could not determine a constructor for the tag !vaultUpon reading a couple of blogs, I understood that I need to specify so…

Accessing a XAMPP mysql via Python

Im attempting to use mysql after only having worked with sqlite in the past.Ive installed XAMPP on Linux (ubuntu) and have mysql up and running fine (seems like that with phpMyadmin at least). However,…

How to use deep learning models for time-series forecasting?

I have signals recorded from machines (m1, m2, so on) for 28 days. (Note: each signal in each day is 360 length long).machine_num, day1, day2, ..., day28 m1, [12, 10, 5, 6, ...], [78, 85, 32, 12, ...],…

Python - Create Shortcut with arguments

Using win32com.client, Im attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.Traceback (most recent call …

How to replace a range of values with NaN in Pandas data-frame?

I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?

django 1.10 Exception while resolving variable is_popup in template admin/login.html

I create a new django project with python3.5 and django1.10.0,I keep getting an error in the admin whenever I want access localhost:8000/admin, He`res the error:[DEBUG]- Exception while resolving varia…

Programmatically extract data from an Excel spreadsheet

Is there a simple way, using some common Unix scripting language (Perl/Python/Ruby) or command line utility, to convert an Excel Spreadsheet file to CSV? Specifically, this one:http://www.econ.yale.e…

Is it possible to specify the driver dll directly in the ODBC connection string?

Im trying to use pyodbc to connect to a SQL Server (MS SQL Server through FreeTDS) in a portable application; since its supposed to be standalone, I would like to avoid having to explicitly install the…

Escape New line character in Spark CSV read

Im working on Spark 2.2.1 version and using the below python code, I can able to escape special characters like @ : I want to escape the special characters like newline(\n) and carriage return(\r). I r…