this is my first question on here. The question is simple –
# this removes the top list from the list of lists
triangle = [
[3, 0, 0],
[2, 0, 0],
[1, 0, 0]]
del triangle[0]
I want to have a similarly easy way to remove a ‘column’. I can of course do this using a for loop, but is there something equivalent to
del triangle[0]
Thankyou
if you want to do this in place without copying the whole list, then something like
EDIT. yes, it won’t work if there’s 0 in the column, just use any other accumulator function
for python 2 where
mapis not an iterator accumulator is not needed:as a side effect, this returns the deleted column which you may use
(for python 2 list() wrapper is not needed)
A shorter form would be
or
Although I’m not sure if it qualifies as ‘without for loop’
P.S. In official Python documentation they use 0-lenqth deque to consume iterators, like this:
I don’t know if it’s better than sum(), but it can be used for non-numeric data