Q:

Python3.x Changing ' ' into an integer/ float. Calculating average from a list

I am trying to change the '' in a list to an integer or float. Lets say I have a list
    allprices = ['', '', '', 1.2, 3.2, 1.8, '', '', '', '', '', '']

I want to change the empty strings into floats or ints I am not sure about that part but pretty sure they have to be floats at the end as I am going to calculate averages. Now for that I have another list called
    averages = []

the list "allprices" is a list of lists of elements so formed by sub lists basically. It has 6 levels. I tried to replace '' with 0 and it worked. But couldn't figure out a way how to make that change with a command. There are some examples and commands I found here or in other forums but didn't work for me. The first thing I tried was 
    var = ''
    var1= 0
    var = var1

but this gives some error. I also tried making it a float directly but didn't work either. Please help or direct me to another title so I can figure it out. BTW i am new to programming so what I try to do might not look like a convenient way to tackle this problem but I am happy as long as it works for me.

A:

Just ignore them in the sum when you calculate the average:
>>> allprices = ['', '', '', 1.2, 3.2, 1.8, '', '', '', '', '', '']
>>> sum(x for x in allprices if x)/len(allprices)
0.5166666666666667

Note - this relies on the 'non truthiness' of ''. If you have list elements that otherwise are 'truthy' (such as ' ')but should be filtered, adjust your if clause appropriately:
>>> allprices = ['', '', '', 1.2, 3.2, 1.8, '', '', '', '', '', ' ']
>>> sum(x for x in allprices if isinstance(x, (float, int)))/len(allprices)
0.5166666666666667

If you might have classes of numbers that are not int or float use the abstract base class Numbers to filter:
>>> import numbers
>>> sum(x for x in allprices if isinstance(x, numbers.Number))/len(allprices)
0.5166666666666667

If you want to actually replace '' with 0 use a list comprehension:
>>> [e if e else 0 for e in allprices]
[0, 0, 0, 1.2, 3.2, 1.8, 0, 0, 0, 0, 0, 0]

or,
>>> [e if isinstance(e, (float, int)) else 0 for e in allprices]
[0, 0, 0, 1.2, 3.2, 1.8, 0, 0, 0, 0, 0, 0]

