Open In App

Getting copy of filtered collection in Julia – filter() and filter!() Methods

Improve
Improve
Like Article
Like
Save
Share
Report

The filter() is an inbuilt function in julia which is used to return a copy of the specified abstract array with different types of filtration performed. These all operation are illustrated with below examples.
 

Syntax: 
filter(f, a::AbstractArray) 
or 
filter(f, d::AbstractDict) 
or 
filter(f, itr::SkipMissing{<:AbstractArray})
Parameters: 
 

  • f: Specified instruction.
  • a::AbstractArray: Specified array.
  • d::AbstractDict: Specified dictionary.
  • itr::SkipMissing: Specified SkipMissing iterator.

Returns: It returns a copy of the specified abstract array with different types of filter operation. 
 

Example 1: This example returns a copy of a, removing elements for which f is false. 
 

Python




# Julia program to illustrate
# the use of filter() method
  
# Getting a copy of specified array,
# removing elements for which f is false.
a = 5:20
println(filter(isodd, a))
println(filter(iseven, a))


Output: 
 

[5, 7, 9, 11, 13, 15, 17, 19]
[6, 8, 10, 12, 14, 16, 18, 20]

Example 2: This example returns a copy of the specified dictionary, removing elements for which f is false. 
 

Python




# Julia program to illustrate
# the use of filter() method
  
# Getting a copy of dictionary, removing
# elements for which f is false
A = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter(p->isodd(p.first), A))
println(filter(p->iseven(p.first), A))


Output: 
 

Example 3: This example returns a vector similar to the array wrapped by the given SkipMissing iterator but with all missing elements and those for which f returns false removed. 
 

Python




# Julia program to illustrate
# the use of filter() method
  
# Getting a vector similar to the array
# wrapped by the given SkipMissing
# iterator but with all missing elements
# and those for which f returns false removed.
A = [5 7; missing 8; 10 11]
println(filter(isodd, skipmissing(A)))
println(filter(iseven, skipmissing(A)))


Output: 
 

 

filter!()

The filter!() is an inbuilt function in julia which is used to update the specified abstract array with different types of filtration performed. These all operation are illustrated with below examples.
 

Syntax: 
filter!(f, a::AbstractArray) 
or 
filter!(f, d::AbstractDict)
Parameters: 
 

  • f: Specified instruction.
  • a::AbstractArray: Specified array.
  • d::AbstractDict: Specified dictionary.

Returns: It returns the updated specified abstract array with different types of filter operation. 
 

Example 1: This example returns updated a, removing elements for which f is false. 
 

Python




# Julia program to illustrate
# the use of filter !() method
  
# Getting updated specified array,
# removing elements for which f is false.
a = Vector(2:10)
println(filter !(isodd, a))
b = Vector(2:10)
println(filter !(iseven, b))


Output: 
 

[3, 5, 7, 9]
[2, 4, 6, 8, 10]

Example 2: This example returns the updated specified dictionary, removing elements for which f is false. 
 

Python




# Julia program to illustrate
# the use of filter !() method
  
# Getting updated dictionary, removing
# elements for which f is false
A = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter !(p->isodd(p.first), A))
B = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter !(p->iseven(p.first), B))


Output: 
 

 



Last Updated : 31 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads