for if else in one line



>>> plate = '3k4j3m'
>>> nmplt=[]
>>> letplt=[]
>>> [letplt.append(let) if(i%2!=0) else nmplt.append(let) for i,let in enumerate(plate)]
[None, None, None, None, None, None]
>>> nmplt
['3', '4', '3']
>>> letplt
['k', 'j', 'm']
>>> ''.join(nmplt)
'343'
>>> ''.join(letplt)
'kjm'

Using only if

>>> [let for i,let in enumerate(plate) if i%2!=0]
['k', 'j', 'm']


Comments

Popular Posts