Suppose I have this code: How can I neatly get a result like this? Answer Note that return 2*x,x is short for return (2*x,x), i.e. a tuple. Your list comprehension thus generates a list of tuples, not a tuple of lists. The nice thing of zip however is that you can easily use it in reverse with the asterisk: Note
Tag: list-comprehension
Python for and if on one line
I have a issue with python. I make a simple list: I want create a “single line code” for find a string. for example, I have this code: But when I watch the variable is wrong (I find the last value of my list): Why does my variable contain the last element and not the element that I want to
Finding prime numbers using list comprehention
I was trying to generate all prime numbers in range x to y. I tried simple example first: range(10,11) which means to check if 10 is a prime number: Here is my code: I know that the thing is missing the option to tell the expression that x%y != 0 should be checked for all y in range (2,x) and
Conditionally add items to a list when defining the list?
Is there a way to add items to a list conditionally, when defining the list? Here’s what I mean: Obviously the above code doesn’t work, but is there a similar way? Currently I have But I was wondering if there’s an other way that wouldn’t required looping through the list, since they can be 10 000 items long and the
Inline for loop
I’m trying to learn neat pythonic ways of doing things, and was wondering why my for loop cannot be refactored this way: I tried replacing the for loop with: But it doesn’t work. The for v in vm: loop evicts numbers from vm based on when they come next in q. Answer What you are using is called a list
Python LINQ like methods
As new to Python i really miss LINQ methods. I’ve found this and this questions, which helped me a lot to understand how Python enumerables and generators work. But sill, I want to use good old methods like Select, SelectMany, First, Last, Group, Distinct and so on. I understand, that all cases can be handled by generator and/or for expressions,
How can I use list comprehensions to process a nested list?
I have this nested list: I want to convert each element in l to float. I have this code: How can I solve the problem with a nested list comprehension instead? See also: How can I get a flat result from a list comprehension instead of a nested list? Answer Here is how you would do this with a nested
How to handle exceptions in a list comprehensions?
I have some a list comprehension in Python in which each iteration can throw an exception. For instance, if I have: I’ll get a ZeroDivisionError exception in the 3rd element. How can I handle this exception and continue execution of the list comprehension? The only way I can think of is to use a helper function: But this looks a
How can I get a flat result from a list comprehension instead of a nested list?
I have a list A, and a function f which takes an item of A and returns a list. I can use a list comprehension to convert everything in A like [f(a) for a in A], but this returns a list of lists. Suppose my input is [a1,a2,a3], resulting in [[b11,b12],[b21,b22],[b31,b32]]. How can I get the flattened list [b11,b12,b21,b22,b31,b32] instead?
How do I remove duplicates from a list, while preserving order?
How do I remove duplicates from a list, while preserving order? Using a set to remove duplicates destroys the original order. Is there a built-in or a Pythonic idiom? Answer Here you have some alternatives: http://www.peterbe.com/plog/uniqifiers-benchmark Fastest one: Why assign seen.add to seen_add instead of just calling seen.add? Python is a dynamic language, and resolving seen.add each iteration is more