I quote this example: 27. Remove Element https://leetcode.com/problems/remove-element/
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
I want to code in my own editor, eg Pycharm. But the skeleton for filling the answer has List type. Sometimes, there is Optional in the argument.
These are not standard Python type. But when used in Leetcode, it works. So everytime, I have to remove them when editing in my own editor.
I’m curious as to why it works in Leetcode but not in my editor ? Is there something I can do on my own editor to use List ? If it’s a specific type on Leetcode, then I’m curious as to why Leetcode does not use the standard python list ?
>Solution :
They are names that must be imported from the typing module:
from typing import List, Optional
You can avoid importing them if you use from __future__ import annotations, which will prevent annotations from being evaluated at runtime, or if you use Python 3.11 (ETA late 2022) or later, in which deferred annotations will be the norm.
typing.List, among others, is deprecated, as Python 3.9 introduced the ability to use list[int] in place of List[int]. Others, like Optional, have no corresponding type in Python proper.