Home > database >  Adding '' to a list?
Adding '' to a list?

Time:02-24

I'm making a little calculator in python. Here's the code so far.

while True:
  operatio = input('Enter here: ')
  operation = operatio   ' '
  tempnumstr = ''
  numlst = []
  operationstr = ''
  for i in operation:
    if i.isnumeric() == True:
      tempnumstr  = i
    else:
      operationstr  = i
      numlst.append(tempnumstr)
      tempnumstr = ''
print(numlst)
print(operationstr)

Basically I'm trying to isolate the numbers and the operation so I can then have it do that operation. If I input 18 12, for example, the output it will give is:

['18', '', '', '12']
   

How do I get rid of the empty apostrophes in the middle of the list? It's posing a problem when I try to add up 18 and 12 later in the program.

CodePudding user response:

Given that numlist will only contain strings, you can use the built-in filter() function:

numlist = filter(None, nulist)

What the above code does is assign a new list to the numlist variable with all the values in numlist excluding the ones with the truthy value of False, such as '', None, 0, [], etc.

CodePudding user response:

Other solution is to remove the blank spaces of numlst manually

while '' in numlst:
    numlst.remove('')
  • Related