# Walrus fails with \[\]

**URL:** <https://discuss.python.org/t/walrus-fails-with/15606>\
**Category:** Python Help\
**Created:** [May 8, 2022, 8:15am UTC](https://discuss.python.org/t/walrus-fails-with/15606 "2022-05-08T08:15:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [May 8, 2022, 8:15am UTC](https://discuss.python.org/t/walrus-fails-with/15606/1 "2022-05-08T08:15:14Z")

</div>

these are invalid,

```auto
([x] := [1])

```

gives,

```auto
SyntaxError: cannot use assignment expressions with list

```

similarly,

```auto
([x, *_] := [1, 2])

```

also fails with the same error.

What could be the reasoning that they decide to not allow such a syntax?  
I do not see any problem with them.

---

<div class="post-metadata">

**Author:** ![sandraC](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@sandraC](https://discuss.python.org/u/sandraC)\
**Post date:** [May 8, 2022, 9:04am UTC](https://discuss.python.org/t/walrus-fails-with/15606/2 "2022-05-08T09:04:23Z")

</div>

To me, it is disabled because it may be a bit confusing(and a bit controversial). Will this expression be equal to `[1]`, or `x` ?  
Just my idea.

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [May 8, 2022, 9:42am UTC](https://discuss.python.org/t/walrus-fails-with/15606/3 "2022-05-08T09:42:52Z")

</div>

what could be controversial here,  
let us say we have,

1. 

```auto
print([[x := [i*2], x] for i in range(1, 5)])

```

which outputs,

```auto
[[[2], [2]], [[4], [4]], [[6], [6]], [[8], [8]]]

```

but if we instead use,

1. 

```auto
print([[[x] := [i*2], x] for i in range(1, 5)])

```

then it should output,

```auto
[[[2], 2], [[4], 4], [[6], 6], [[8], 8]]

```

but the second one is not valid for some unknown reason.

---

<div class="post-metadata">

**Author:** ![sandraC](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@sandraC](https://discuss.python.org/u/sandraC)\
**Post date:** [May 8, 2022, 10:06am UTC](https://discuss.python.org/t/walrus-fails-with/15606/4 "2022-05-08T10:06:53Z")

</div>

I will try to explain my perspective but, again, it is just my thought.  
You have : `[x] := [i*2]` and I found 2 different scenarios (with two different results) about what the output may be:

### Scenario 1

It will return `x,` which is equal to `i*2`.

### Scenario 2

It will return what you have typed before the operator(since you haven’t written just ‘x’, but `[x]`):  
It will return `[x]` which is equal to `[i*2]`

~~ 
### Scenario 3
 It will return what you have typed after operator(value) : `[i*2]` ~~

Note: Found last scenario ridiculous and decided to remove it.

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [May 8, 2022, 10:24am UTC](https://discuss.python.org/t/walrus-fails-with/15606/5 "2022-05-08T10:24:54Z")

</div>

I was investigating this a bit more,  
it turns out that I cannot use walrus with tuple also,  
so,

```auto
print([[(x, y) := (i*2, i*3), x, y] for i in range(1, 5)])

```

gives,

```auto
SyntaxError: cannot use assignment expressions with tuple

```

but the way I want it to work is, it outputs,

```auto
[[(2, 3), 2, 3], [(4, 6), 4, 6], [(6, 9), 6, 9], [(8, 12), 8, 12]]

```

I assume, when I write,

```auto
[[[x] := [i*2], x] for i in range(1, 5)]

```

then the, `[x] := [i*2]` part should do two things,

1. return `[i*2]` (that is, whatever comes after the walrus operator)
2. set `[x] = [i*2]`

and this value of `x` would be used in the second entry of the list in my previous post, that is here,

```auto
[[x] := [i*2], x]
               ^

```

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 8, 2022, 10:29am UTC](https://discuss.python.org/t/walrus-fails-with/15606/6 "2022-05-08T10:29:39Z")

</div>

Remember: the `:=` operator does _not_ do anything that isn’t possible without it, so what would be the code _without_ using the **assignment expression operator**?

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [May 8, 2022, 10:40am UTC](https://discuss.python.org/t/walrus-fails-with/15606/7 "2022-05-08T10:40:16Z")

</div>

It is a deliberate design. See the [PEP](https://peps.python.org/pep-0572/#differences-between-assignment-expressions-and-assignment-statements):

“Iterable packing and unpacking (both regular or extended forms) are not supported”

The reason isn’t given. If you really care, you could dig through the discussions on Python-Ideas and Python-Dev mailing lists, but be warned, there are hundreds of posts.

I expect that the reason is one or both of:

- too hard to get the parser to understand it;
- too confusing to read for the human reader.

The walrus operator was hugely controversial, it was so stressful with [so much opposition and negativity](https://www.blog.pythonlibrary.org/2018/07/13/guido-retire-as-bdfl/) that it drove Guido into retiring as the BDFL of the Python language. So it may be that this was just a compromise position, prohibiting a minor corner case that increases complexity without actually being very useful.

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [May 8, 2022, 10:58am UTC](https://discuss.python.org/t/walrus-fails-with/15606/8 "2022-05-08T10:58:46Z")

</div>

without using the walrus, one would have to recompute,

```auto
[[(i*2, i*3), i*2, i*3] for i in range(1, 5)]

```

```auto
[[[i*2], i*2] for i in range(1, 5)]

```

(had to recompute `i*2`, `i*3`)

the other way to do this could be,

```auto
[[(x := i*2, y := i*3), x, y] for i in range(1, 5)] # but had to repeat walrus

```

```auto
[[[x := i*2], x] for i in range(1, 5)]

```

maybe they do not want to make it more complicated, as for someone reading walrus for the first time, it would be a bit confusing on its own.  
but if overtime walrus becomes more popular, then I think they will have to reconsider iterable packing and unpacking.

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 8, 2022, 11:18am UTC](https://discuss.python.org/t/walrus-fails-with/15606/9 "2022-05-08T11:18:25Z")

</div>

When I code, I always keep in mind: _The Zen of Python_  
In particular, these points:

- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Readability counts.

I can see that ‘walrus’ can address some of those points.

The biggest issues will be backwards compatibly. There are many systems that are still using Python2.x, and a huge number that have yet to move away from Python 3.6, so I think it’ll be years down the road before we see ‘walrus’ being used in general.

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [May 8, 2022, 6:54pm UTC](https://discuss.python.org/t/walrus-fails-with/15606/10 "2022-05-08T18:54:50Z")

</div>

> [@vainaijr](#):
>
> without using the walrus, one would have to recompute,
> 
> ```auto
> [[(i*2, i*3), i*2, i*3] for i in range(1, 5)]
> 
> ```

There is no need to recompute:

```auto
stream = ((i*2, i*3) for i in range(1, 5))
data = [[[x,y], x,y] for x, y in stream]

[[[2, 3], 2, 3], [[4, 6], 4, 6], [[6, 9], 6, 9], [[8, 12], 8, 12]]

```

Based on that one can construct hard to read (but without recompute) one-liner as well:

```auto
data = [[[x, y], x, y] for x, y in ((i*2, i*3) for i in range(1, 5))]
```

---

<div class="post-metadata">

**Author:** ![thatbirdguythatuknownot](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/thatbirdguythatuknownot/32/7172_2.png) [@thatbirdguythatuknownot](https://discuss.python.org/u/thatbirdguythatuknownot)\
**Post date:** [May 8, 2022, 9:41pm UTC](https://discuss.python.org/t/walrus-fails-with/15606/11 "2022-05-08T21:41:04Z")

</div>

> **Scenario 2**
> 
> It will return what you have typed before the operator (since you haven’t written just ‘x’, but `[x]`):  
> It will return `[x]` which is equal to `[i*2]`

That scenario is the simple and consistent way to do it. It’s also how chained assignment works, so shouldn’t it also be how the assignment expression works?
