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-
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)