python - Remove an element from list and return the result -
i want generate list looks like:
['ret-120','ret-115','ret-110',....'ret-5','ret5',....'ret240']
please note, there's no ret0
element in list. have remove list populated range function. i've tried:
['ret'+str(x) x in list(range(-120,241,5)).remove(0)]
however gives out error:
typeerror: 'nonetype' object not iterable
is possible accomplish 1 line of code?
the simplest way want add conditional inside list comprehension:
lst = ['cumret'+str(x) x in xrange(-120,241,5) if x != 0] # part: ^^^^^^^^^
i removed unnecessary list creation , changed range
->xrange
(note range
->xrange
change python2 only)
Comments
Post a Comment