In computer programming, a collection is a grouping of some variable number of data items (possibly zero) that have some shared significance to the problem being solved and need to be operated upon together in some controlled fashion. [1]
This page provides syntax for different types of collections and data structures in Julia (arrays, sets, dictionaries, etc.). Each section includes an example to demonstrate the described methods.
Arrays are ordered collection of elements. In Julia
they are automatically indexed (consecutively numbered) by an integer starting with 1.
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Input:
Output:
Sets are an unordered collection of unique elements.
Input:
Output:
Dictionaries are unordered collection of key-value pairs where the key serves as the index (“associative collection”). Similar to elements of a set, keys are always unique.
Input:
Output:
Wikipedia contributors (n.d.). Collection. In Wikipedia. Retrieved May 1, 2024, from https://en.wikipedia.org/wiki/Collection_(abstract_data_type)
Julia Documentation: Base - Collections and Data Structures
Think Julia: Chapter 10 - Arrays
Think Julia: Chapter 11 - Dictionaries
Think Julia: Chapter 12 - Tuples
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
New array (empty)
[]
Specify type (integer)
Int64[]
Specify type (string)
String[]
Array with values
[1, 2, 3, 4, 5]
Array with values
["a1", "b2", "c3"]
Array of numbers
collect(1:10)
Split string str
by delimiter into words (e.g., space)
split(str, " ")
Get length of array my_array
length(my_array)
Get first element of array my_array
my_array[1]
Get last element of array my_array
my_array[end]
Get n element of array my_array (e.g., 2)
my_array[2]
Check if element is in array
in(str, my_array)
Add element to end
push!(my_array, str)
Remove element from end
pop!(my_array)
Remove element from beginning
popfirst!(my_array)
Add element to beginning
pushfirst!(my_array, str)
Sort array (will not change array itself)
sort(my_array)
Sort array in place (will change array)
sort!(my_array)
Get unique elements in array
unique(my_array)
Intersection
intersect(my_array, your_array)
Union
union(my_array, you_array)
Convert array to string
join(collect(my_array), str)
New set (empty)
Set[]
Specify type
Set{Int64}
Set with values
Set([1, 2, 3, 4, 5])
Set with values
Set(["a1", "b2", "c3", "b2"])
Get length of set my_set
length(my_set)
Check if value is in set
in(str, my_set)
Add value
push!(my_set, str)
Intersection
intersect(my_set, your_set)
Union
union(my_set, your_set)
Difference
setdiff(my_set, your_set)
New dictionary (empty)
Dict[]
Specify type
Dict{String, Int64}
Dictionary with values
Dict("one" => 1 , "two" => 2, "three" => 3, "four" => 4)
Get value for key in dictionary my_dict
my_dict["one"]
Check if dictionary has key
haskey(my_dict, "one")
Check for key/value pair
in(("one" => 1), my_dict)
Get value and set default
get!(my_dict, "one", 5)<br>get!(my_dict, "five", 5)
Add key/value pair
my_dict["five"] = 5
Delete key/value pair
delete!(my_dict, "four")
Get keys
keys(my_dict)
Get values
values(dict)
Convert keys to array
collect(keys(my_dict))
Convert values to array
collect(values(my_dict))
Sorting keys
sort(collect(keys(my_dict)))
Sorting values
sort(collect(values(my_dict)))
Sort by value (descending) with keys
sort(collect(zip(values(my_dict), keys(my_dict))), rev=true)
Sort by value (ascending) with keys
sort(collect(zip(values(my_dict), keys(my_dict))), rev=false)
Get top n by value (e.g., 3)
sort(collect(zip(values(my_dict), keys(my_dict))), rev=true)[1:3]