Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove ".keyword" if it is present in particular element in list

I am having list as below, in which one element contains "hostname.keyword".
I want to remove ".keyword" from that element in the list.

I checked with below code but its not working.

search_columns=['service', 'perf', 'hostname.keyword']

if search_columns:
        if ".keyword" in search_columns:
            search_columns = search_columns.replace(".keyword","")
            print(search_columns)

Expected output-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

search_columns=['service', 'perf', 'hostname']

Update-

Working code-

char = ".keyword"
for idx, ele in enumerate(search_columns):
    search_columns[idx] = ele.replace(char, '')

Ref link- https://www.geeksforgeeks.org/python-remove-given-character-from-strings-list/

>Solution :

you idea is almost right, but you forgot to iterate in each element of your array before looking for the ".hostname". Try this:

search_columns=['service', 'perf', 'hostname.keyword']

if search_columns:
    for index, searchTerm in enumerate(search_columns):
        if ".keyword" in searchTerm:
            search_columns[index] = searchTerm.replace(".keyword","")
            
print(search_columns)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading