By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Understanding list comprenesions (split from Copying the list elements)

dshimer
Expert 100+
P: 136
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
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
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 5K+
P: 6,596
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.