Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extracting elements over multiple lists?

Reply
Thread Tools

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?




 
Reply With Quote
 
 
 
 
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"

 
Reply With Quote
 
 
 
 
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









 
Reply With Quote
 
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
 
Reply With Quote
 
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"

 
Reply With Quote
 
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.

 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
newb: recurse over elements children and disable all form elements SteveKlett@gmail.com Javascript 4 09-20-2006 07:42 AM
extracting link from elements content smarto59@hotmail.com XML 0 02-12-2005 11:32 AM
Rubish Way of extracting elements Daniel Völkerts Ruby 12 08-18-2004 03:24 PM



Advertisments