def words(string: str) -> list[str]:
return string.split(' ')
Running this raises the error
TypeError: 'type' object is not subscriptable
How do I correctly specify that the output will be a list of strings?
For now, you need to write the following:
from typing import List
def words(string: str) -> List[str]:
return string.split(' ')
Note the capitalisation of List
vs list
. There’s a PEP for making your code work going forward, starting with Python 3.9.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With