1

I'm new to python. I'm trying to learn data extracting from an Excel file. I encountered the following statement:

sheet_data = [[sheet.cell_value(r, col) for col in range(sheet.ncols)] for r in range(sheet.nrows)]

I understand regular for loops, but not the below version:

x for y in range()

What does it mean when you have a variable x before the for y in range()?

5
  • 2
    This is called list comprehension if you want to google it. Commented Nov 11, 2015 at 1:36
  • 1
    x for y in range is not valid syntax. Commented Nov 11, 2015 at 1:38
  • @preezzzy I found it. docs.python.org/2/tutorial/… Commented Nov 11, 2015 at 1:41
  • That should get you rolling. @cricket_007 is right about the the invalid syntax by the way. Commented Nov 11, 2015 at 1:43
  • @cricket_007 Right. I wasn't sure what to call it before, but now I know. Commented Nov 11, 2015 at 1:46

3 Answers 3

1

The for statement is used for looping over a list. This is referred to as an iterator. When it is encompassed by [..], this is referred to as a list comprehension.

List comprehensions allow you to transform one list into another. This is sometimes referred to as a mapping i.e. mapping from X -> Y where a function transforms the value of X into the returned value of Y

So, for example, in

[y + 2 for y in range(...)]

the for is iterating over all values in the list produced by the range(). Each list element has 2 added to each value of y, so the final result is a list where each element is 2 greater than the corresponding element in the source list. Thus, range(3) would produce [0, 1, 2] which then transforms into [2, 3, 4].

So [y for y in range(..)] wouldn't actually accomplish much.

I see that in the example you have provided there are two iterators, which complicates things a bit. But essentially, they are providing two reference variables: r and col, from which the final result is derived using these two variables.

List comprehensions are a very powerful tool in Python. Definitely worth knowing.

Sign up to request clarification or add additional context in comments.

1 Comment

Pedantic: "List comprehensions allow you to transform one iterable into a new list." range itself isn't even a list in Py3 for instance.
0

These are called list comprehensions in python. If you have a function do_something then the following two blocks are equivalent:

result = [do_something(y) for y in range(10)]

...

result = []
for y in range(10):
    result.append(do_something(y))

Where range(10) could be any iterable.

Think of them as quick ways to create lists. They work for dictionaries too as of python 2.7. This tutorial may be helpful.

1 Comment

Where is x defined?
0

The "x" is an arbitrary variable name that holds the values of the sequence object. Using it in a list comprehension or in a generator expression will return the items in the iterable object that is being stepped through.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.