Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Extracting elements over multiple lists? (http://www.velocityreviews.com/forums/t805662-extracting-elements-over-multiple-lists.html)

 JoeM 11-07-2011 05:12 PM

Extracting elements over multiple lists?

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 11-07-2011 05:37 PM

Re: Extracting elements over multiple lists?

> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 Laurent Claessens 11-07-2011 05:44 PM

Re: Extracting elements over multiple lists?

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 11-07-2011 06:01 PM

Re: Extracting elements over multiple lists?

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 11-07-2011 06:22 PM

Re: Extracting elements over multiple lists?

> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 Peter Otten 11-07-2011 06:33 PM

Re: Extracting elements over multiple lists?

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

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 11-07-2011 06:51 PM

Re: Extracting elements over multiple lists?

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 11-07-2011 08:06 PM

Re: Extracting elements over multiple lists?

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 11-08-2011 12:06 AM

Re: Extracting elements over multiple lists?

On 11/7/2011 1:22 PM, John Gordon 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.

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 11-08-2011 07:07 AM

Re: Extracting elements over multiple lists?

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

All times are GMT. The time now is 05:03 PM.