Home > Software design >  how can I convert values based on condition from list in python?
how can I convert values based on condition from list in python?

Time:03-16

I'm new to Python. I just faced one problem, which was taken place when I tried to convert specific values from list.

[[20170702, 20170708],
 993,
 994,
 995,
 996,
 997,
 998,
 999,
 ...]

What I want is, I'd like to convert all these integers to 0 except list things. I tried to get through this problem with using np.where and other ways but all failed.

CodePudding user response:

if your list is called x then code to get desired result is:

new_list = [0 if type(element) != list else element for element in x]

CodePudding user response:

r = [0 if isinstance(i,int) else i for i in llist]
  • Related