0

I am a novice in python. I wanted to create a list in a single line:

>>> x = [[-1, y] for y in range(-1, 2), [0, 1], [1, z] for z in range(1, -2, -1), [0, -1]]

to get :

>>>> x = [[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]]

but it gave error : name 'z' is not defined. So I had to do it with for loops and single pairs on separate lines, appending each time. Before that I did :

>>> x = [[-1, y] for y in range(-1, 2),[0, 1]]
>>> x
[[-1, [-1, 0, 1]], [-1, [0, 1]]]

which tells me y is taken as iterator of for loop as well as [0, 1]. I am wrongly corelating comma in C and python. How do I achieve what I intend to do (in a single line) ? (I know that I could do it directly since its a small range. But what if there was a bigger one?)

2
  • Is the order random on purpose? I initially thought you meant to perform [ [y,z] for y in range(-1, 2) for z in range(-1, 2)]. Please explain yourself better. Commented May 17, 2015 at 13:36
  • A list comprehension produces a homogenous list. You are trying to create a heterogenous list, one that contains both lists of ints (e.g. [-1, -1]) and lists of lists of ints ([[1,1], [1,0], [1, -1]]). Commented May 17, 2015 at 13:54

2 Answers 2

2

You should concatenate lists, and put nested list to brackets

>>> [[-1, y] for y in range(-1, 2)] + [[0, 1], [[1, z] for z in range(1, -2, -1)], [0, -1]]
[[-1, -1], [-1, 0], [-1, 1], [0, 1], [[1, 1], [1, 0], [1, -1]], [0, -1]]
Sign up to request clarification or add additional context in comments.

Comments

0

Check out itertools. Not sure from your question whether you're going to want product, permutations or combinations but I think one of those will be what you need.

edit: On closer inspection, you're doing something much simpler and you just missed a few brackets. Try:

x = [[[-1, y] for y in range(-1, 2)], [0, 1], [[1, z] for z in range(1, -2, -1)], [0, -1]]

List comprehensions go in their own set of brackets, it's similar to the syntax of a list literal but not the same.

edit 2:

Before that I did :

x = [[-1, y] for y in range(-1, 2),[0, 1]]
x [[-1, [-1, 0, 1]], [-1, [0, 1]]]

which tells me y is taken as iterator of for loop as well as [0, 1]. I am wrongly corelating comma in C and python. How do I achieve what I intend to do (in a single line) ? (I know that I could do it directly since its a small range. But what if there was a bigger one?)

Yes you're misunderstanding what your comma is doing there. It's turning the 2nd part of your for loop into a tuple of (range(-1,2), [0, 1]) (2 items). What you needed to do here was close your list comprehension after range(-1,2), and then wrap the whole thing in square brackets to make it a literal list declaration:

x = [[[-1, y] for y in range(-1, 2)],[0, 1]]

As mentioned by @YuriyKovalev, it would probably be clearer to concat the lists like so:

x = [[-1, y] for y in range(-1, 2)] + [0, 1]

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.