467,915 Members | 1,318 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Understanding list comprenesions (split from Copying the list elements)

dshimer
Expert 100+
Barton, let me jump in here for a second. The syntax you explained to me recently works for this, then you can remove something from l2 without it affecting l1 (this is just stating the facts). However while trying my newest favorite cool python trick, I noticed that when I do it as listed below it returns the list of "None"s which don't really affect anything and the copied list is fine, but the question is why, and how do you avoid them.
Expand|Select|Wrap|Line Numbers
  1. >>> l1=[1,2,3,4]
  2. >>> l2=[]
  3. >>> [l2.append(i) for i in l1]
  4. [None, None, None, None]
  5. >>> l2
  6. [1, 2, 3, 4]
  7. >>> l2.remove(3)
  8. >>> l1
  9. [1, 2, 3, 4]
  10. >>> l2
  11. [1, 2, 4]
  12. >>> 
May 7 '07 #1
  • viewed: 944
Share:
2 Replies
bartonc
Expert 4TB
Barton, let me jump in here for a second. The syntax you explained to me recently works for this, then you can remove something from l2 without it affecting l1 (this is just stating the facts). However while trying my newest favorite cool python trick, I noticed that when I do it as listed below it returns the list of "None"s which don't really affect anything and the copied list is fine, but the question is why, and how do you avoid them.
Expand|Select|Wrap|Line Numbers
  1. >>> l1=[1,2,3,4]
  2. >>> l2=[]
  3. >>> [l2.append(i) for i in l1]
  4. [None, None, None, None]
  5. >>> l2
  6. [1, 2, 3, 4]
  7. >>> l2.remove(3)
  8. >>> l1
  9. [1, 2, 3, 4]
  10. >>> l2
  11. [1, 2, 4]
  12. >>> 
Expand|Select|Wrap|Line Numbers
  1. l2.append(i)
is a function.
None is what that function returns. The fact that the fuction succeeded in modifying its (mutable) object is not reflected in the return value.

The thing about list comprehensions is that they act like loops, but are not always appropiate to use in place of a loop. So, if you want a list of things generated by a comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> l = [i for i in range(4)]
  2. >>> l
  3. [0, 1, 2, 3]
  4. >>> 
May 7 '07 #2
bartonc
Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. l2.append(i)
is a function.
None is what that function returns. The fact that the fuction succeeded in modifying its (mutable) object is not reflected in the return value.

The thing about list comprehensions is that they act like loops, but are not always appropiate to use in place of a loop. So, if you want a list of things generated by a comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> l = [i for i in range(4)]
  2. >>> l
  3. [0, 1, 2, 3]
  4. >>> 
A really neat part of comprehensions it the 'predicate':
Expand|Select|Wrap|Line Numbers
  1. >>> l = [i for i in range(4) if (i % 2) == 0]
  2. >>> l
  3. [0, 2]
  4. >>> 
May 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Keith Davies | last post: by
9 posts views Thread by danny van elsen | last post: by
18 posts views Thread by Simon | last post: by
22 posts views Thread by greenflame | last post: by
6 posts views Thread by Fazana | last post: by
35 posts views Thread by Lee Crabtree | last post: by
7 posts views Thread by john.ford | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.