Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python annotation's type for nested list with mix of numbers and lists

It is known that an annotation’s type for [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is List[List[int]]. But what is the annotation’s type for nested list with mix of numbers and lists [[1, 1], 2, [1 , 1]]?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Typing has evolved a lot in recent python version, after all it was introduced recently, therefore depending on which version you’re using, the typing signature will be different.

Type of [[1, 2, 3], [4, 5, 6], [7, 8, 9]]:

python version type
>= 3.6 typing.List[typing.List[int]]
>= 3.9 list[list[int]]

Type of [[1, 1], 2, [1 , 1]]:

python version type
>= 3.6 typing.List[typing.Union[typing.List[int], int]]
>= 3.9 list[typing.Union[list[int], int]]
>= 3.10 list[list[int] | int]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading