аватар question@mail.ru · 01.01.1970 03:00

Removing empty elements of the Python list

Sorry for the stupid question, but how to remove empty elements from the list in the Pieton?

аватар answer@mail.ru · 01.01.1970 03:00

For starters, it is worth formalizing what "empty" element is

"" empty "" can be considered many elements, for example,

  [], {},  set  (),  ''    

including

   none ,  0 ,  0.0 ,  0j ,  false      

class = ""> for value in values ​​ if value]

or, if the list is not required, generator expression:

  (Value  for  value  in  values ​​ if  if  

If the rule of determining the "empty" "element is more complicated, it is enough to describe this rule after the operator if in the above expressions, for example,

  [Value  for  value  in  values ​​ if  value! = [ none ]]     

will leave elements in the list that are not equal to the list containing none (it is important to notice that such a list will be missed into the final set in the first cases).

Latest

Similar