1

I have python code

my_list = ['hsdpa_-_rl_fail', 'avg_reported_cqi.', 'fach-c_load_ratio', 'canc_isho_cpich/ecno_nrt_m1010c209']
my_dict = {'-' : 'dash' , '.':'dot' , '/':'slash'}

I want search and replace in my_list as per mapping given in my_dict that is my_dict key shall be replaced by respective value in all element of my_list.

How this can be done in pythonic way?

Bharat Sharma
  • 1,081
  • 3
  • 11
  • 23

1 Answers1

2

Using a simple Iteration.

Ex:

my_list = ['hsdpa_-_rl_fail', 'avg_reported_cqi.', 'fach-c_load_ratio', 'canc_isho_cpich/ecno_nrt_m1010c209']
my_dict = {'-' : 'dash' , '.':'dot' , '/':'slash'}

for i, v in enumerate(my_list):
    for k in my_dict:
        if k in v:
            my_list[i] = v.replace(k, my_dict[k])

print(my_list)

Output:

['hsdpa_dash_rl_fail', 'avg_reported_cqidot', 'fachdashc_load_ratio', 'canc_isho_cpichslashecno_nrt_m1010c209']
Rakesh
  • 81,458
  • 17
  • 76
  • 113