How to use Julia arrays
Learn how to add, delete and replace items in Julia arrays; how to find and remove duplicates in an array; how to join or intersect two arrays, and more.
Updated October 27, 2023Tested with Julia version 1.9.3
What is an array in the Julia language?
Like in many programming languages, in Julia, an array is an ordered collection, or list, of items. You create arrays in Julia with square brackets, with each item separated with a comma:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
How to get the first “n” items in an array in Julia?
To get the first item in a Julia array, we simply do this:
first(planets)
We get:
"Mercury"
To get the first “n” items in a Julia array, we add “n” as an optional second argument to first()
. If we want the first three planets, for example, we’d input:
first(planets, 3)
We get:
3-element Vector{String}:
"Mercury"
"Venus"
"Earth"
How to get the last “n” items in an array in Julia?
To get the last item in a Julia array, we simply do this:
last(planets)
We get:
"Pluto"
To get the last “n” items in a Julia array, we add “n” as an optional second argument to last()
. For instance, if we want the last four planets in our array, we input:
last(planets, 4)
We get:
4-element Vector{String}:
"Saturn"
"Uranus"
"Neptune"
"Pluto"
How to remove the first item from a Julia array
Let’s say the Sun has grown in size and swallowed Mercury. Now we need to delete Mercury (the first element in our array) from our planets
array. We can do this using a function called popfirst!()
:
popfirst!(planets)
In the terminal, we see the planet we just deleted:
"Mercury"
And if we input planets
, we get:
8-element Vector{String}:
"Venus"
"Earth"
"Mars"
"Jupiter"
"Saturn"
"Uranus"
"Neptune"
"Pluto"
Note that our planets
array has been made smaller in a persistent way—the popfirst!()
function modifies its arguments.
How to remove the last item from a Julia array
Since Pluto is now officially a “dwarf planet” rather than a regular planet, perhaps it shouldn’t be in our planets
array either.
To remove the last item from an array, we can use pop!()
, like this:
pop!(planets)
Julia will show us the planet we just deleted:
"Pluto"
Now when we have Julia show us the updated array:
planets
… we get:
7-element Vector{String}:
"Venus"
"Earth"
"Mars"
"Jupiter"
"Saturn"
"Uranus"
"Neptune"
Bye Pluto. We can see that Mercury isn’t there either—we permanently deleted it in the section above.
How to remove an item from a Julia array by index number
Let’s create a new array:
astronauts = ["Buzz Aldrin", "Neil Armstrong", "Michael Collins", "Yuri Gagarin"]
What if we now want to delete, say, the second astronaut in our array?
We can do this with the deleteat!
function. (It looks like the function name is “delete eat” but it’s actually “delete at” 😂).
deleteat!(astronauts, 2)
We get:
3-element Vector{String}:
"Buzz Aldrin"
"Michael Collins"
"Yuri Gagarin"
You can also delete a range of elements from an array using deleteat!()
in Julia.
Let’s start with our original planets again. By inputting the code below, we’ll be overwriting our seven-planet array with our original nine-planet array.
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
If we want to delete planets three to seven inclusive, we can do this:
deleteat!(planets, 3:7)
We see the remaining planets:
4-element Vector{String}:
"Mercury"
"Venus"
"Neptune"
"Pluto"
How to delete elements from an array by name in Julia
There isn’t a simple way to delete an element by name in Julia. However, there are a few slightly complex approaches. Of these, the one I like is below.
Say we have the following array:
rockets = ["Apollo", "Saturn", "Falcon Heavy"]
deleteat!(rockets, findall(x->x=="Saturn",rockets))
We get:
2-element Vector{String}:
"Apollo"
"Falcon Heavy"
How to keep only certain elements in a Julia array
Now that Julia 1.7 has been released, we can opt to keep only certain elements in a Julia array with keepat!()
.
For example, say we’re using our original array of rockets:
rockets = ["Apollo", "Saturn", "Falcon Heavy"]
We can keep just the second rocket:
keepat!(rockets, 2)
We get:
1-element Vector{String}:
"Saturn"
If we want to keep more than one element, we can do so by passing in an array instead of an integer. If we start with our original astronauts:
astronauts = ["Buzz Aldrin", "Neil Armstrong", "Michael Collins", "Yuri Gagarin"]
We can keep the second and fourth astronaut like this:
keepat!(astronauts, [2,4])
We get:
2-element Vector{String}:
"Neil Armstrong"
"Yuri Gagarin"
You can also pass a range of element positions to keepat!()
.
Let’s start with our original planets array:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
If we want to keep only planets three to five (inclusive), we do this:
keepat!(planets, 3:5)
We get:
3-element Vector{String}:
"Earth"
"Mars"
"Jupiter"
How to add an item to an array in Julia
Let’s start with our original planets array (including Pluto) again:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
And let’s pretend NASA (with a little help from SpaceX and Blue Origin) have just discovered a new planet, Mythos. To add it to our planets
array, we use push!()
:
push!(planets, "Mythos")
We get:
10-element Vector{String}:
"Mercury"
"Venus"
"Earth"
"Mars"
"Jupiter"
"Saturn"
"Uranus"
"Neptune"
"Pluto"
"Mythos"
How to create a Julia array with the numbers in a given range
If you want a Julia array with all the numbers from 1 to 5:
collect(1:5)
We get:
5-element Array{Int64,1}:
1
2
3
4
5
If we want to count by anything other than 1, we can put the increment size between our start and end numbers.
For example, if we want an array with the numbers from 1 to 5, counting by 2, we do this:
collect(1:2:5)
We get:
3-element Vector{Int64}:
1
3
5
How to find and replace with Julia arrays
Time for a new array:
array_1 = ["Houston", "Washington", "orlando"]
Notice that the first two items are in title case, while "orlando"
is in lowercase. Let’s make it consistent using a find and replace.
We can do this:
array_1 = replace(array_1, "orlando" => "Orlando")
Now when we input array_1
into our Julia terminal, we get:
3-element Vector{String}:
"Houston"
"Washington"
"Orlando"
Voila!
However, we can also use the bang function replace!()
, which will make our code more concise. Let’s use another array to demonstrate:
array_2 = ["Gagarin", "Armstrong", "aldrin"]
This time, let’s use the bang function to make the last item in title case:
replace!(array_2, "aldrin" => "Aldrin")
When we input array_2
into our Julia terminal, we get:
3-element Vector{String}:
"Gagarin"
"Armstrong"
"Aldrin"
Just what we wanted.
How to use regex with “find and replace” on arrays in Julia
Using regex to find and replace within arrays in Julia requires a slightly different approach. Let’s work with an example.
Let’s input a third array into our terminal:
array_3 = ["fish", "fishes", "goldfish", "cat"]
Let’s normalise this data so that each of the different types of fish end up as simply "fish"
.
The way to do this in Julia is as follows:
array_3 = replace.(array_3, r"(gold)?fish(es)?" => s"fish")
When we input array_3
into our Julia terminal, we get our desired result:
4-element Vector{String}:
"fish"
"fish"
"fish"
"cat"
Let’s walk through the code above — including the regex — and then we’ll get to some approaches that don’t work.
array_3 =
Here we are reassigningarray_3
to the result of our forthcoming find-and-replace. This will make the change persist.
replace.(
Here we call the built-in Julia functionreplace
. The dot.
afterreplace
indicates we’re using the “broadcast” version of the function. This means that our operation will apply to each item in the array.Next, we open the parentheses so we can add our arguments.
array_3
We tell Julia where to perform the find-and-replace — the proverbial haystack.
r"(gold)?fish(es)?"
We now tell Julia what we’re looking for — the proverbial needle.In this case, it’s a regex (denoted by the letter
r
immediately before an opening double quote mark).Inside the double quotes, we have the our string
fish
, surrounded by the optional prefixgold
(as ingoldfish
) and the optional suffixes
(as infishes
). If you’re not familiar with regex, it can be a tricky subject, but the question mark?
means the preceding text is optional. And because we’ve wrappedgold
andes
in parentheses, we’ve made each a separate optional “unit”.
=>
The “fat arrow” or “stabby arrow” means replace the “needle” with whatever comes next. In this case, it’s …
s"fish"
— Here, we’re telling Julia our desired replacement. Thes
stands for “substitution”.
)
Here we close the parentheses for thereplace.
function.
Approaches that don’t work for using regex to find and replace in Julia arrays
There are a couple of approaches that won’t work here.
First, let’s reset our array:
array_3 = ["fish", "fishes", "silverfish", "cat"]
Now let’s try the “reassign” approach that worked for non-regex find-and-replace on arrays earlier in this tutorial:
array_3 = replace(array_3, r"(gold)?fish(es)?" => s"fish")
This doesn’t do anything; if we input array_3
into our Julia terminal, we get the unwanted original values:
4-element Vector{AbstractString}:
"fish"
"fishes"
"goldfish"
"cat"
Okay, what about the “bang function” approach that also worked for non-regex find-and-replace on arrays earlier in this tutorial? Let’s try that:
replace!(array_3, r"(gold)?fish(es)?" => s"fish")
That doesn’t work either; when we input array_3
, we still get the same unchanged result:
4-element Vector{AbstractString}:
"fish"
"fishes"
"goldfish"
"cat"
The takeout here is that when using regex to find and replace in Julia arrays, we need to use the “broadcast” version of the replace function, as described at the top of this section. Even though we can get away without using it for non-regex find-and-replace operations on Julia arrays, we need the broadcast-enabled replace.()
when using regex.
How to get the union of two arrays in Julia
Let’s say we have two arrays:
planets_a = ["Mars", "Earth", "Neptune"]
planets_b = ["Pluto", "Earth", "Jupiter"]
Now let’s say we want to see all the elements in both arrays, deduped (note that "Earth"
appears in both arrays).
We can do that like this:
array_union = union(planets_a, planets_b)
We get:
5-element Array{String,1}:
"Mars"
"Earth"
"Neptune"
"Pluto"
"Jupiter"
Notice that the result is deduped—we only get "Earth"
once, even though it appeared in both arrays.
How to get the intersection of two arrays in Julia
We can also get the intersection of two arrays in Julia.
This means we want to see only the element(s) that appear in both arrays.
We do that like this:
array_intersect = intersect(planets_a, planets_b)
The result:
1-element Array{String,1}:
"Earth"
"Earth"
was the only element that appeared in both arrays, so it is the only element that appears in the intersection.
How to get the opposite of the intersection on two arrays in Julia (symmetric difference)
If you want to get the opposite of the intersection of two arrays in Julia—items that appear in one array or the other but not both, then you need to use symdiff()
.
Here’s an example. Let’s say we have the following two arrays. You’ll notice the only common item is Apollo
:
rockets = ["Apollo", "Saturn", "Falcon Heavy"]
greek_gods = ["Apollo", "Zeus", "Hera"]
We run the symdiff function to get all the non-common items:
symdiff(rockets, greek_gods)
The result:
4-element Vector{String}:
"Saturn"
"Falcon Heavy"
"Zeus"
"Hera"
Apollo
is the only item in both arrays, and thus it is the only item not to appear in our result.
How to get the items that exist in one Julia array but not the other
Here are our two planet arrays again:
planets_a = ["Mars", "Earth", "Neptune"]
planets_b = ["Pluto", "Earth", "Jupiter"]
Let’s say we want the items that exist in planets_a
, but don’t appear in planets_b
.
We do that like this:
setdiff(planets_a, planets_b)
… and we get:
2-element Array{String,1}:
"Mars"
"Neptune"
The order matters here. If we reverse the order of the arrays inside the setdiff
function, like this:
setdiff(planets_b, planets_a)
… we get:
2-element Array{String,1}:
"Pluto"
"Jupiter"
How to check if a value exists in an array
Let’s begin with the O.G. nine planets again:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
If you wanted to check if Pluto was included in our planets
array (hey, you never know these days), you can do that like this:
"Pluto" in planets
We get a result of true
, which tells us that Pluto does in fact exist in our array.
Conversely:
"Krypton" in planets
leads to a result of false
.
How to handle uppercase and lowercase when checking if a value exists in an array
Using the example above, if we had instead checked if "pluto"
(lowercase) existed in the planets
array:
"pluto" in planets
then we’d get a result of false
, because "pluto"
is not the same thing as the "Pluto"
.
However, you can handle lowercasing elegantly like this:
"pluto" in lowercase.(planets)
This gives us a result of true
, because we told Julia to lowercase the planets
array before checking for "pluto"
.
In the code above, we’re using the lowercase()
function, which turns strings to lowercase in Julia, but we’ve added a dot immediately after lowercase
. This dot tells Julia to convert each item in the array to lowercase, rather than to the array itself (which isn’t possible and leads to a MethodError
). This trailing dot is known as a “broadcast function”, and it’s built in to Julia—no package required.
You can similarly handle uppercase inconsistencies:
"PLUTO" in uppercase.(planets)
gives us a result of true
.
How to deduplicate a Julia array
This one’s pretty simple. Let’s say you have an array with duplicates:
space_companies = ["SpaceX", "Blue Origin", "Boeing", "Sierra Nevada Corporation", "Boeing"]
(We have Boeing twice.)
Here’s how you dedupe it in Julia:
space_companies_deduped = unique(space_companies)
This gives you the deduped array:
4-element Vector{String}:
"SpaceX"
"Blue Origin"
"Boeing"
"Sierra Nevada Corporation"
Or if you wanted to overwrite the original array with the deduped list, you could do it like this:
unique!(space_companies)
Now when you call up space_companies
, you get this:
4-element Vector{String}:
"SpaceX"
"Blue Origin"
"Boeing"
"Sierra Nevada Corporation"
How to find duplicates in a Julia array without removing them
Let’s say you want to know if you have any duplicates in your Julia array (and how many of each duplicate) but you don’t want to remove the duplicates just yet.
You can do that a few different ways, but the easiest seems to be by installing a package called StatsBase.jl
and using its built-in function called countmap()
.
To add the package, at the Julia prompt hit the right square bracket ]
and you’ll see the green Julia prompt turn into this blue one:
pkg>
Now we can instruct the package manager to:
add StatsBase
(You don’t need the .jl
suffix.)
Now Julia’s package manager will add the StatsBase package for you. Once done, it will revert to the blue prompt again:
pkg>
To escape out of the package manager, just hit Backspace.
You should see the green Julia prompt again:
julia>
Now we need to tell Julia that we want to actually use the package:
using StatsBase
(We don’t need the .jl
suffix here either.)
Now we can identify our duplicates.
Let’s say we want to use the original space_companies
array from the previous example, before we deduplicated it.
We input that into Julia like this:
space_companies = ["SpaceX", "Blue Origin", "Boeing", "Sierra Nevada Corporation", "Boeing"]
We get:
5-element Vector{String}:
"SpaceX"
"Blue Origin"
"Boeing"
"Sierra Nevada Corporation"
"Boeing"
To identify the duplicates in this array without removing them, we can use the countmap()
function from the StatsBase package:
countmap(space_companies)
We get a dictionary with the frequency of each string in the array, like so:
Dict{String, Int64} with 4 entries:
"Boeing" => 2
"Sierra Nevada Corporation" => 1
"Blue Origin" => 1
"SpaceX" => 1
We can see that "Boeing"
is duplicated – it appears twice. We can also see that all the other strings occur only once.
If you want to do more with this dictionary, such as writing it to a file, you can assign it to a variable, like this:
space_companies_countmap = countmap(space_companies)
Speaking of writing Julia arrays to a file…
How to write a Julia array to a text file or CSV
Let’s say you want to write the contents of a Julia array to a text file.
Let’s use this array:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
We write the text file like this:
using DelimitedFiles
writedlm("/Users/ron/Desktop/planets.txt", planets)
… or if you want a CSV instead of a text file, simply change the extension of the filename:
using DelimitedFiles
writedlm("/Users/ron/Desktop/planets.csv", planets)
The result is a file named planets.txt
(or planets.csv
depending on what you chose above) on my desktop, the complete contents of which are:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Let’s walk through the code:
using DelimitedFiles
Here we’re telling Julia that we want to use a package that ships with Julia by default—it’s calledDelimitedFiles
. This package can read from and write to text files.
writedlm("/Users/ron/Desktop/planets.csv", planets)
Now we use a built-in function from theDelimitedFiles
package—it’s calledwritedlm()
and it writes to a text file for us.The first argument (
"/Users/ron/Desktop/planets.csv"
) inside the function contains the details of the file we want to create. Obviously you’ll need to edit the path, the file name, and the file extension (e.g..txt
or.csv
) to suit yourself.The second argument (
planets
) is the array we want to write into our text file or CSV.
How to read a text file line by line to create a Julia array
Let’s say you have a file named planets.txt
, and that its complete contents are as follows:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Now let’s say you want to create a Julia array from this file.
The quickest way I’ve found to do this is as follows:
using DelimitedFiles
planets_matrix = readdlm("/Users/ron/Documents/planets.txt")
planets_array = vec(planets_matrix)
We get:
8-element Vector{Any}:
"Mercury"
"Venus"
"Earth"
"Mars"
"Jupiter"
"Saturn"
"Uranus"
"Neptune"
Let’s walk through the above code real quick:
using DelimitedFiles
The “DelimitedFiles” package is built in to Julia i.e. you don’t have to install it. However, you do need to tell Julia you want to use it—hence this line. The reason we want to use this package is that it enables writing and reading from files such as the.txt
file we’re using in this example.
planets_matrix = readdlm("/Users/ron/Documents/planets.txt")
Skipping past the variable name (planets_matrix
) for a minute, we’re using a function namedreaddlm()
that ships with theDelimitedFiles
package.This function will read the contents of our text file and store it in a matrix. Don’t worry, I know we want an array—we’ll convert the matrix into an array in the next step.
For clarity’s sake, we’ll name this variable
planets_matrix
.
planets_array = vec(planets_matrix)
In this step we take the matrix we created in the previous line (planets_matrix
) and convert it to an array using Julia’s nativevec()
function. We store the result in a new variable namedplanets_array
.
N.B. This code will give us an array of type Any
, but an array nonetheless. Even if you double-quote each planet’s name in the original text file and re-run the code, the result is still an array of type Any
.