When I assign a list to variable why Pycharm give me a prompt that is "this list creation could be rewritten as a list literal"?
I am a Python beginner and have a puzzle. When I write code like this:
lst = [1, 2, 3, 4]
Pycharm give me a prompt that is "this list creation could be rewritten as a list literal". But if it's replaced by
lst = list([1, 2, 3, 4])
Pycharm doesn't say anything. Who could tell me why?
Is this code like
lst = [1, 2, 3, 4] legal in Python? Can I ignore prompt?
Check your code to make sure you don't have
lst somewhere else as
If you type the following:
lst=  # more code lst = [1, 2, 3, 4]
You'll receive the prompt you got. You won't run into problems if you keep it that way but it's bad practice.
In those two cases you are using a function to change the variable:
append(). In the previous one where you're just redefining the variable explicitly.
Another improper example:
a = 7 # some code that has nothing to do with "a" or uses it a = 8
a = 8 to begin with. No need to store
a = 7.