Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty string in list in Julia

Tags:

list

julia

I am looking for efficient solution to remove empty string in a list in Julia.

Here is my list :

li = ["one", "two", "three", " ", "four", "five"]

I can remove empty string by using for loop, as following :

new_li = []
for i in li
    if i == " "
    else
        push!(new_li, i)
    end
end

But I believe there is more efficient way to remove the empty string.

like image 385
Chanhee Avatar asked Nov 08 '21 02:11

Chanhee


People also ask

How do I remove blank strings from a list?

Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.

How do you remove Blank values from a list?

Using filter() method Just filter out the None and empty element form list. If None is used as the first argument to filter() , it filters out every value in the given list, which is False in a boolean context. This includes empty lists.

How do you remove null from a list in Python?

The easiest way to remove none from list in Python is by using the list filter() method. The list filter() method takes two parameters as function and iterator. To remove none values from the list we provide none as the function to filter() method and the list which contains none values.

How to delete an element by name in a Julia array?

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. Now that Julia 1.7 has been released, we can opt to keep only certain elements in a Julia array with keepat! ().

How do I get the last n items in a Julia array?

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: 4-element Vector {String}: "Saturn" "Uranus" "Neptune" "Pluto"

How do I extract a character from a string in Julia?

If you want to extract a character from a string, you index into it: Many Julia objects, including strings, can be indexed with integers. The index of the first element (the first character of a string) is returned by firstindex (str), and the index of the last element (character) with lastindex (str).

How to remove all the elements from a list in Java?

The removeAll () method is commonly used to remove all the elements from the list that are contained in the specified collection. For example, the following code removes all strings that are null or empty from a list of strings: 2. Using List.removeIf () method


Video Answer


2 Answers

new_li = filter((i) -> i != " ", li)

or

new_li = [i for i in li if i != " "]
like image 113
Milan Lakhani Avatar answered Oct 19 '22 17:10

Milan Lakhani


I have a couple of comments a bit too long for the comments field:

Firstly, when building an array, never start it like this:

new_li = []

It creates a Vector{Any}, which can harm performance. If you want to initialize a vector of strings, it is better to write

new_li = String[]

Secondly, " " is not an empty string! Look here:

jl> isempty(" ")
false

It is a non-empty string that contains a space. An empty string would be "", no space. If you're actually trying to remove empty strings you could do

filter(!isempty, li)

or, for in-place operation, you can use filter!:

filter!(!isempty, li)

But you're not actually removing empty strings, but strings consisting of one (or more?) spaces, and maybe also actually empty strings? In that case you could use isspace along with all. This will remove all strings that are only spaces, including empty strings:

jl> li = ["one", "", "two", "three", " ", "four", "   ", "five"];

jl> filter(s->!all(isspace, s), li)
5-element Vector{String}:
 "one"
 "two"
 "three"
 "four"
 "five"
like image 37
DNF Avatar answered Oct 19 '22 18:10

DNF