List Comprehension Without [ ] in Python

List comprehension without [ ] in Python

>>>''.join( str(_) for _ in xrange(10) )

This is called a generator expression, and is explained in PEP 289.

The main difference between generator expressions and list comprehensions is that the former don't create the list in memory.

Note that there's a third way to write the expression:

''.join(map(str, xrange(10)))

Python List Comprehension and 'not in'

Try this:

[x for x in t if x not in s]

You can nest any for if statements in list comprehensions. Try this identation, to get really long chains of conditionals, with a clearer intuition about what the code is doing.

my_list = [(x,a)
for x in t
if x not in s
if x > 0
for a in y
...]

See?

list comprehension equivalent without producing a throwaway list

I don't now if you will find it elegant, but there is a consume recipe in the itertools docs that is very fast and will run an iterator to completion without building-up a list:

>>> consume(some_func(x) for x in some_list if x>5)

python list comprehension without in

So the syntax here is a little confusing, but what's actually happening is that each item in c.items() is a tuple containing a word and its count.

A more clear way of writing this would be:

vocab = [x for (x, count) in c.items() if x>=2]

but it could be also be done like this:

vocab = [x[0] for x in c.items() if x[1]>=2]

where x is a tuple.

It can also be helpful to look at what c actually looks like. If you print c, you see:

>>> print c
Counter({'lie': 3, 'is': 2, 'and': 1, 'a': 1, 'There': 1, 'only': 1, 'passion': 1, 'piece': 1})

and c.items()

>>> print c.items()
[('and', 1), ('a', 1), ('lie', 3), ('is', 2), ('There', 1), ('only', 1), ('passion', 1), ('piece', 1)]

python - list comprehension without assignment

This has been asked many times, e.g., here and here. But it's an interesting question, though. List comprehensions are meant to be used for something else.

Other options include

  1. use map() - basically the same as your sample
  2. use filter() - if your function returns None, you will get an empty list
  3. Just a plain for-loop

while the plain loop is the preferable way to do it. It is semantically correct in this case, all other ways, including list comprehension, abuse concepts for their side-effect.

In Python 3.x, map() and filter() are generators and thus do nothing until you iterate over them. So we'd need, e.g., a list(map(...)), which makes it even worse.

Conditional list comprehension without else

List comprehensions support the inclusion of a predicate to filter the items.

I think you want:

ans = [x[0] for x in zip(a,b) if x[1]==minimum]

or maybe a little clearer like this:

ans = [x for (x,y) in zip(a,b) if y==minimum]

List comprehension without saving it vs single line for loop

The first is absolutely the best choice of the four you list.

Don't use a list comprehension unless you actually want the list it produces, and don't cram a for loop on to one line just to make it a one-liner. Having two loops seems a definite drawback to the fourth; you are repeating yourself a bit more than necessary, although you should profile to see if the double iteration makes a significant difference in the runtime. I suspect you would need to be generating very long lists before the difference between one loop and two really matters, though.


However, there is one option you have overlooked: producing a sequence of tuples, then "unzipping" it into a tuple of tuples.

sq, div2 = zip(*((i**2, i/2) for i in range(10)))

I think I would still prefer to see the first one, though. It's clearer, without requiring the reader to recognize the unzip idiom.

Get a list (without nested list) directly from a list comprehension

Try this with just zip to get it in that order that you want:

[i for j in zip(alist1_temp, alist2_temp) for i in j]

if you don't mind the order just do:

alist1_temp + alist2_temp

or get it with itertools.chain thanks to @buran:

import itertools

list(itertools.chain(alist1_temp, alist2_temp))

Use list comprehension without iteration variable

Just discard the value of i:

a = [random.randrange(-10, 11) / 10 for _ in range(100)]

_ is considered the "last value" in Python and by convention is
used as a "throw away" value.



Related Topics



Leave a reply



Submit