Velocity Reviews > Extracting elements over multiple lists?

# Extracting elements over multiple lists?

JoeM
Guest
Posts: n/a

 11-07-2011
Howdy,

If I have a few lists like

a=[1,2,3,4,5]
b=["one", "two", "three", "four", "five"]
c=["cat", "dog", "parrot", "clam", "ferret"]

what is the most pythonic method of removing the first element from
all of the lists?

A list comprehension such as [arr[1:] for arr in a,b,c]
gives a single 2d list, which is not what I'm shooting for. Any
suggestions?

John Gordon
Guest
Posts: n/a

 11-07-2011
In <(E-Mail Removed)> JoeM <(E-Mail Removed)> writes:

> a=[1,2,3,4,5]
> b=["one", "two", "three", "four", "five"]
> c=["cat", "dog", "parrot", "clam", "ferret"]

> what is the most pythonic method of removing the first element from
> all of the lists?

for arr in [a,b,c]:
arr.pop(0)

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Laurent Claessens
Guest
Posts: n/a

 11-07-2011
Le 07/11/2011 18:12, JoeM a écrit :
> Howdy,
>
> If I have a few lists like
>
> a=[1,2,3,4,5]
> b=["one", "two", "three", "four", "five"]
> c=["cat", "dog", "parrot", "clam", "ferret"]
>
> what is the most pythonic method of removing the first element from
> all of the lists?

Do you want to remove the first item of each list, or to create new
lists that contain the same as a,b,c but with one element less ?

Something like what you wrote :
[arr[1:] for arr in a,b,c]
will create *new* lists.

Assuming you don't want new lists, I would do :

a=[1,2,3,4,5]
b=["one", "two", "three", "four", "five"]
c=["cat", "dog", "parrot", "clam", "ferret"]

for x in [a,b,c]:
x.remove(x[0])

print a
print b
print c

I think that writing
>>> [x.remove(x[0]) for x in [a,b,c]]

instead of the for loop is cheating ... but it also does the job.

Have a good after noon
Laurent

JoeM
Guest
Posts: n/a

 11-07-2011
Thanks guys, I was just looking for a one line solution instead of a
for loop if possible. Why do you consider

[x.remove(x[0]) for x in [a,b,c]]

cheating? It seems compact and elegant enough for me.

Cheers

John Gordon
Guest
Posts: n/a

 11-07-2011
In <(E-Mail Removed)> JoeM <(E-Mail Removed)> writes:

> Thanks guys, I was just looking for a one line solution instead of a
> for loop if possible. Why do you consider

> [x.remove(x[0]) for x in [a,b,c]]

> cheating? It seems compact and elegant enough for me.

I wouldn't call it cheating, but that solution does a fair bit of
unneccessary work (creating a list comprehension that is never used.)

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Peter Otten
Guest
Posts: n/a

 11-07-2011
JoeM wrote:

> Thanks guys, I was just looking for a one line solution instead of a
> for loop if possible. Why do you consider
>
> [x.remove(x[0]) for x in [a,b,c]]
>
> cheating? It seems compact and elegant enough for me.

I think it's a misconception that you are avoiding the for-loop. You move it
into [...] and declare it more elegant, but in reality you are creating a
throwaway list of None-s. You are adding cruft to your code.

That is not only superfluous, but also misleading. A simple for-loop like

for x in a, b, c:
del x[0]

on the other hand makes your intention crystal-clear.

Jean-Michel Pichavant
Guest
Posts: n/a

 11-07-2011
JoeM wrote:
> Thanks guys, I was just looking for a one line solution instead of a
> for loop if possible. Why do you consider
>
> [x.remove(x[0]) for x in [a,b,c]]
>
> cheating? It seems compact and elegant enough for me.
>
>
>
> Cheers
>

This is a one liner, but since you asked something *pythonic*, John's
solution is the best imo:

for arr in [a,b,c]:
arr.pop(0)

(Peter's "del" solution is quite close, but I find the 'del' statement
tricky in python and will mislead many python newcomers)

JM

Dave Angel
Guest
Posts: n/a

 11-07-2011
On 11/07/2011 01:01 PM, JoeM wrote:
> Thanks guys, I was just looking for a one line solution instead of a
> for loop if possible. Why do you consider
>
> [x.remove(x[0]) for x in [a,b,c]]
>
> cheating? It seems compact and elegant enough for me.
>
>
>
> Cheers

Are you considering the possibility that two of these names might
reference the same list?

a = [42, 44, 6, 19, 48]
b = a
c = b

for x in [a,b,c]:
x.remove(x[0])

now a will have [19,48] as its content.

--

DaveA

Terry Reedy
Guest
Posts: n/a

 11-08-2011
On 11/7/2011 1:22 PM, John Gordon wrote:
> In<(E-Mail Removed)> JoeM<(E-Mail Removed)> writes:
>
>> Thanks guys, I was just looking for a one line solution instead of a
>> for loop if possible. Why do you consider

>
>> [x.remove(x[0]) for x in [a,b,c]]

>
>> cheating? It seems compact and elegant enough for me.

It looks like incomplete code with 'somelists = ' or other context
omitted. It saves no keypresses '[',...,SPACE,...,']' versus
....,':',ENTER,TAB,... . (TAB with a decent Python aware editor.)

> I wouldn't call it cheating, but that solution does a fair bit of
> unneccessary work (creating a list comprehension that is never used.)

The comprehension ( the code) is used, but the result is not. If the
source iterator has a large number of items rather than 3, the throwaway
list could become an issue. Example.

fin = open('source.txt')
fout= open('dest.txt, 'w')
for line in fin:
fout.write(line.strip())
# versus
[fout.write(line.strip()) for line in fin]

If source.txt has 100 millions lines, the 'clever' code looks less
clever ;=). Comprehensions are intended for creating collections (that
one actually wants) and for normal Python coding are best used for that.

--
Terry Jan Reedy

Laurent Claessens
Guest
Posts: n/a

 11-08-2011
Le 07/11/2011 19:01, JoeM a écrit :
> Thanks guys, I was just looking for a one line solution instead of a
> for loop if possible. Why do you consider
>
> [x.remove(x[0]) for x in [a,b,c]]
>
> cheating? It seems compact and elegant enough for me.

I have the feeling that it does not do what I expect it does just by
seeing the line. It is list comprehension, but the point is absolutely
not in creating a list.

I'd say it breaks the rule «Explicit is better than implicit.» while
«Special cases aren't special enough to break the rules.»

But well... could be a matter of taste; I prefer the loop.

Laurent