0
[['user_1',
  'user_2',
  'user_8',
   None,
   None],
 ['ben',
  'karl',
   None,
   None]]

I try to remove the missing values

for element in df:
     element=[x for x in element if x is not None]

this code leave everything as it was

Ekaterina
  • 305
  • 1
  • 4
  • 10
  • You could use [**`filter`**](https://docs.python.org/2/library/functions.html#filter), although that would also remove empty strings, zeroes, etc. – Peter Wood Jan 11 '17 at 09:54
  • 1
    You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder.. And here's a condensed version of the same info, with cute diagrams: [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) – PM 2Ring Jan 11 '17 at 10:14
  • FWIW, none of the answers in that dupe target show how to mutate the existing list (or its sublists) rather than replacing it. – PM 2Ring Jan 11 '17 at 10:20

4 Answers4

1
my_list= [['user_1',
  'user_2',
  'user_8',
   None,
   None],
 ['ben',
  'karl',
   None,
   None]]

print [ [ elt for elt in a_list if elt is not None ] for a_list in my_list ]

[['user_1', 'user_2', 'user_8'], ['ben', 'karl']]

YOBA
  • 2,759
  • 1
  • 14
  • 29
  • 2
    This would also remove list items with `0` in them... – l'L'l Jan 11 '17 at 09:47
  • @l'L'l right, fixed. – YOBA Jan 11 '17 at 10:00
  • This is fine if `len(my_list)` is small, but if it's huge it's probably desirable to save RAM by mutating the existing list rather than replacing it. OTOH, replacing `my_list` _is_ a little faster than mutating it. – PM 2Ring Jan 11 '17 at 10:11
1

The problem with your code is that the line

element=[x for x in element if x is not None]

creates the new filtered list and binds it to the name element, which replaces the old list object that the name element was bound to. But we can use slice assignment to make it mutate that list instead:

df = [
    [
        'user_1',
        'user_2',
        'user_8',
        None,
        None,
    ],
    [
        'ben',
        'karl',
        None,
        None,
    ]
]

# Remove the `None` values
for element in df:
     element[:] = [x for x in element if x is not None]

for row in df:
    print(row)

output

['user_1', 'user_2', 'user_8']
['ben', 'karl']
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
1
for element in df:
     element[:]=[x for x in element if x is not None]

out:

[['user_1', 'user_2', 'user_8'], ['ben', 'karl']]

Document: enter image description here

宏杰李
  • 11,820
  • 2
  • 28
  • 35
0

Reassigning element does not alter the list, because element is a separate variable.

With a small change, you can generate a new outer list using a nested comprehension, something like this:

df = [[x for x in element if x is not None] for element in df]
khelwood
  • 55,782
  • 14
  • 81
  • 108