Reputation: 2912
If I search the Julia 1.0.0 on-line documentation for list
, the first hit is:
Unordered lists can be written by prepending each item in a list with either *, +, or -.
This is in a topic titled “Markdown syntax”, clearly not related to Python-style lists.
Another hit, further down is:
Noteworthy differences from Python (section)
I did not see the word list
mentioned in this section.
A search for “list comprehension”, provided the 10th hit as:
Comprehensions (section)
starting with,
Comprehensions provide a general and powerful way to construct arrays.
The mention of arrays seems to imply Python-style list comprehensions can be done with Julia arrays.
Down one heading is Generator Expressions
Comprehensions can also be written without the enclosing square brackets, producing an object known as a generator.
This sounds much like a Python-style generator.
Do the developers of Julia consider Julia's arrays (1-dim) to be essentially equivalent to Python lists?
Upvotes: 0
Views: 201
Reputation: 2912
TL;DR: Yes, but faster.
@StefanKarpinski, co-creator of Julia, made the following comment here
Another difference is that you're using a set in Python and an array in Julia (what Python calls a ‘list’)
So according to Stefan, “an array” (probably means 1-dimensional array) is “what Python calls a ‘list’”
For a simple example, suppose we want to find the product, P
, of 10 million ratios, R
, where the numerator of each term is 4n^2
, and the denominator of each term is one less than the numerator, R = num/(num - 1)
, where n
= 1 to 10 million.
Without trying to do any optimizations of any kind here is a Python-style list comprehension in Julia 1.0.0:
julia> P = prod([4n^2/(4n^2 - 1) for n in 1:10_000_000])
1.5707962875230395
Running this in the Julia 1.0.0 REPL is very fast, essentially instantaneous on my laptop. This is not a benchmark test, but running similar code, modified for Python, with Python 3.6 in the REPL in PyCharm is notably slower.
import numpy as np
P = np.prod([4*n**2/(4*n**2 - 1) for n in range(1, 10_000_001)])
P
1.5707962875404267
Just visually guessing at speed, Julia is much more than 10 times faster for this list comprehension.
What about generators in Julia?
Just remove the square brackets inside the prod
function call:
julia> P = prod(4n^2/(4n^2 - 1) for n in 1:10_000_000)
1.5707962875404267
Julia’s answer is instantaneous on my laptop.
This is not a speed comparison between Python and Julia, so I will leave Python at this point.
Julia’s 2n
for 2 * n
is a nice feature. For example, try 2P
:
julia> 2P
3.1415925750808533
See here.
Upvotes: 1