Q:

"for in" with unused variable

I need to generate a string with length x out of characters in y.
My straightforward approach was
''.join(random.choice(y) for i in xrange(x))

The problem with this is that i is unused.
Is there a better way to do this?

A:

There is no better way; you can name the variable _ to indicate it is ignored:
''.join(random.choice(y) for _ in xrange(x))

_ is just a convention; experienced programmers reading your code will understand that it signifies 'not used' here, Python doesn't care either way.
From a performance perspective, using a list comprehension here happens to be faster:
''.join([random.choice(y) for _ in xrange(x)])

because the implementation requires two scans to first determine the output length first; this double scan means any generator expression is turned into a list anyway. Using a list comprehension here short-cuts that conversion and is faster.

