473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Understanding list comprenesions (split from Copying the list elements)

dshimer
136 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
2 1105
bartonc
6,596 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
6,596 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

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

Similar topics

0
by: Keith Davies | last post by:
Hi All, In my web page generation, I want to be able to automatically split (create a new page) on nodes that meet certain criteria. One is if the 'out:split' attribute is set to 'yes' (dead...
9
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
18
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
22
by: greenflame | last post by:
I would like to make a function that takes a list, more specificaly a list of strings, and shuffles its elements, like a pile of cards. The following is a script I tryed to make that implements...
6
by: Fazana | last post by:
I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........ Here is the question for the program Question. Part 1. Add the function...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
6
by: Lisa | last post by:
I am reading in data from a text file. I want to enter each value on the line into a list and retain the order of the elements. The number of elements and spacing between them varies, but a...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
7
by: john.ford | last post by:
I want to take a long alpha-numeric string with \n and white-space and place ALL elements of the string (even individual parts of a long white-space) into separate list elements. The most common...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.