Assign Python list fragment from lambda

I know that there are certain “special” methods of various objects that represent operations that are usually performed with operators (that is, int.__add__for +, object.__eq__for ==, etc.) and that one of them list.__setitemthat can assign a value to an element list. However, I need a function that can assign a list to a piece of another list.

Basically, I am looking for an equivalent expression some_list[2:4] = [2, 3].

+3
source share
2 answers

Line

some_list[2:4] = [2, 3]

also cause list.__setitem__(). However, instead of an index, it will pass an object slice. Line is equivalent

some_list.__setitem__(slice(2, 4), [2, 3])
+8
source

Python. 3.2, __setitem__ :

. . a [1: 2] = b a [ (1, 2, )] = b . None.

0

Source: https://habr.com/ru/post/1793470/


All Articles