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

Leetcode. What are the function arguments types?

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.

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

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.

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