Q:

Replace none value with values in the same list

Hi i'm new to python and i'm strugglin with this example.. I want to replace empty elements with the dates in list. For example every empty element replaced by monday until tuesday ... I'm using python 2.7.
this the list that i get :
[u'Monday 30 March', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'Tuesday 31 March', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'Wednesday  1 April', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'']

this is what i tried :
for tim in time_d:
    if tim =='':
        print time_d[0]

A:

Are you looking for sth. like the following:
for i, val in enumerate(time_d):
    if not val:
        time_d[i] = time_d[i-1]

time_d
# ['Monday 30 March', 'Monday 30 March', ... ,'Monday 30 March', 
#  'Tuesday 31 March', 'Tuesday 31 March', ... , 'Tuesday 31 March', 
#  'Wednesday  1 April', 'Wednesday  1 April', ..., 'Wednesday  1 April']

This requires the first value to be set, otherwise you'd have to handle the IndexError.

