[0, 0, 0, 1, 1, 1, 0] ... remove all 0 values

D

Daniel Austria

Hi python - hackers,

just one question. How can i remove all 0 values in a list? Sure - i
can loop over it, but that s not a neat style. list.remove() will
only remove the first occurence. Doing that while no exception is
raised is also uncool, right?

Some suggestions?


Best,
Dan
 
B

Bruno Desthuilliers

Daniel Austria a écrit :
Hi python - hackers,

just one question. How can i remove all 0 values in a list? Sure - i
can loop over it, but that s not a neat style. list.remove() will
only remove the first occurence. Doing that while no exception is
raised is also uncool, right?

Some suggestions?

the_list = [0, 0, 0, 1, 1, 1, 0]

# Simple solution
print [x for x in the_list if x != 0]

# if you want to mutate the list 'in place':
the_list[:] = [x for x in the_list if x != 0]
print the_list

HTH
 
D

D'Arcy J.M. Cain

filter(lambda x: x, your_list)

Or...

[x for x in your_list if x]

I'm not sure which one is more efficient but I like the syntax of the
latter. A smart person could probably figure it out even without
knowing Python syntax. Clarity is trump.
 
D

Diez B. Roggisch

Daniel said:
Hi python - hackers,

just one question. How can i remove all 0 values in a list? Sure - i
can loop over it, but that s not a neat style.

Why not? If you need to potentially look at *all* elements of a list,
nothing but a loop will take you there.

OTOH, your proposed "remove until nothing is found"-thingy will become
quadratic in behavior, as remove also loops over the list - so if you have
list with say 10 ones followed by 10 zeros, you will loop ten times for 11
elements, which is in the order of (n/2)**2.

Diez
 
J

J Kenneth King

Friðrik Már Jónsson said:
Good call! Equivalent but more efficient:

filter(None, your_list)

Regards,
Friðrik Már

I was wondering when someone would mention filter()
 
F

Friðrik Már Jónsson

J said:
I was wondering when someone would mention filter()

I was happy to see that too.

It's clean, faster than list comprehension and in terms of clarity
it's only to be expected that the developer is familiar with, or at
least willing to look up, the available built-in methods.

Regards,
Friðrik Már
 
S

Simon Forman

Hi python - hackers,

just one question. How can i remove all 0 values in a list? Sure - i
can loop over it, but that s not a neat style.  list.remove() will
only remove the first occurence. Doing that while no exception is
raised is also uncool, right?

Some suggestions?

Best,
Dan

If you are doing something like this:

L = [0, 0, 0, 1, 1, 1, 0]
removeZeros(L)
number_of_ones = len(L)

you can just use sum() like so:

number_of_ones = sum(L)



HTH
 
P

Paul Rubin

Daniel Austria said:
just one question. How can i remove all 0 values in a list?

I prefer:

newlist = list(x for x in oldlist if x != 0)

to the square bracket list comprehension that a few people have
suggested. This is because in python 2.x, the listcomp "leaks" its
index variable into the surrounding scope, but the generator
expression above doesn't. Usually not a big deal, but an extra bit of
hygiene never(?) hurts.
 
S

Stefan Behnel

Paul said:
I prefer:

newlist = list(x for x in oldlist if x != 0)

to the square bracket list comprehension that a few people have
suggested. This is because in python 2.x, the listcomp "leaks" its
index variable into the surrounding scope, but the generator
expression above doesn't.

As you indicated, that's been fixed in Py3, though. So if your code works
in Py3.x, you can be somewhat sure that the leak doesn't have side effects.
Plus, it's pretty easy to ignore those leaks as long as you use a suitable
name for the loop variable.

Also note that the performance characteristics may not be identical in both
cases, depending on where you run your code. Cython, for example, will
write a list comprehension out as a rather straight C loop, but when we
implement generator expressions in Cython, it may have to get turned into a
generator function instead of a loop, so that you'd get a much larger
overhead than for the plain list comprehension (although in the simple case
above it would likely get optimised away).

CPython shows a similar difference:

$ python3.1 -m timeit '[x for x in range(1000) if x]'
10000 loops, best of 3: 170 usec per loop
$ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \
'[x for x in r if x]'
1000 loops, best of 3: 222 usec per loop

$ python3.1 -m timeit 'list(x for x in range(1000) if x)'
1000 loops, best of 3: 227 usec per loop
$ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \
'list(x for x in r if x)'
1000 loops, best of 3: 280 usec per loop


Not that I'd consider those numbers worth bothering...

Stefan
 
D

Daniel Austria

Thanks a lot for your advices,

i decided to use the filter() method to sort out the 0.
i can ´t use the sum() function cause i need the list afterwards ....


best,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top