["Hello World"] I was thinking of using regex, but is there a more easy way?" /> ["Hello World"] I was thinking of using regex, but is there a more easy way?" /> ["Hello World"] I was thinking of using regex, but is there a more easy way?"/>

How to remove currency amounts from a list? ["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"] -> ["Hello World"]

63 views Asked by At

["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"] -> ["Hello World"]

I was thinking of using regex, but is there a more easy way?

2

There are 2 answers

0
2080 On

Assuming this is Python, a simple List Comprehension should suffice:

lst = ["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"]
[el for el in lst if "€" not in el]
>>> ["Hello World"]

Another option is using the filter() function:


def contains_currency(s):
    return "€" not in s

list(filter(contains_currency, lst))

more compactly using an anonymous function:

list(filter(lambda s: "€" not in s, lst))
0
ChenBr On

Assuming this is Javascript, a simple filter() should suffice:

let array = ["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"];

filteredArray = array.filter((item) => !item.includes('€'));

console.log(filteredArray);
// ["Hello World"]

Read more about the includes() method here.