Post #2386756
2026-05-07 02:01 UTC
I learned today from a coworker that in python you can use slice indexing to insert a list into another list. For example:
```
a = [0,1,2,3]
b = [8,9]
a[1:1] = b
print(a)
# [0, 8, 9, 1, 2, 3]
```
It works but seems strange to me. It makes sense that the index 1 position of the `a` list gets filled in with the first entry from the `b` list, but then this previously index 1 value is remembered and gets appended to the end of the `b` list. It's not obvious to me.
I intend to look into the implementation to see how it works under the hood.
#TIL
#python
Replies (2)
-
@Blujean@mas.to 2026-05-07 02:57
@davidruffner@raphus.social interesting, thank you. I wondered about the logic of the order as well.
-
@zopyx@mastodon.world 2026-05-07 04:34
@davidruffner@raphus.social That's "mutable slice assignment"