Python Forum

Full Version: Sub: Python-3: Better Avoid Naming A Variable As list ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
While examining various alternatives for extracting dictionary content into a list, it is seen that in some of the web sites, list itself gets used as the name of a variable. For example:

"""
Python Code-01-Start
"""
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }  
list = [] 

for i in dict: 
   k = (i, dict[i]) 
   list.append(k) 
  
print(list) 
#Output: [('Geeks', 10), ('for', 12), ('Geek', 31)]

"""
Python Code-01-End
"""
On its own, this code snippet works fine & gives the desired result. However, any subsequent code snippet involving use of list() function, attracts error. Sample code:

"""
Python Code-02-Start
""" 
mdict = {"A":50,"B":60,"C":70}

keylist = list(mdict)
print(keylist)

"""
Python Code-02-End
"""
Error:
Error Message on IDLE Python Shell-3.7.4 Traceback (most recent call last): File "<<>>", line <<>>, in <module> keylist = list(mdict) TypeError: 'list' object is not callable
If in first code block we replace the variable name list by mlist, the problem disappears and we get the correct output for second code snippet too i.e.
Output:
#Output: ['A', 'B', 'C']
Conclusion: Even though list does not formally feature in Python-3 keywords, and there are some instances of its actually being used as variable name, it would seem prudent to avoid doing so.

Esteemed members might like to look into it & offer their views.

adt
That apply not only to list but to all built in functions, modules, packages, etc.
list = [] is a beginner error, no advanced programmer would do such silly naming.
Your final conclusion is correct, but list is a Python keyword and should not be used as a variable name.
Thanks buran, Thomast - for your kind inputs.

It is rather odd that a prominent web site did adopt this style in its sample code.
We are keen to know what you know so please name the website and where to find such crappy code.
GeeksForGeeks

The Link is given above. Overall, the web site appears to be quite helpful.
tuple, list, set, dict, ... are builtin types. Their name can be overwritten.
The name is only a reference to an object. A object can have more than one reference.
The assignment to a name, in this case with a dict or a list, let point the name to the new created object.
The old reference to dict and list is no longer there.

To demonstrate this with code:
print(dict)
print(list)

dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }  
list = []


print(dict)
print(list)

# deleting the names
del dict
del list
# now magically the old types come back
print(dict)
print(list)


# demonstrating syntax highlighting 
value = 42
print(value)
# value is black
# dict and tuple not, because they are built-ins
PS: If you use an IDE or Editor with syntax highlighting, you'll be warned by colors, that you're using a built-in name.
If you look here, you see that dict and list have another color.
(Aug-28-2019, 05:03 PM)adt Wrote: [ -> ]Yoriz wrote 2 hours ago:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.

I am thankful to you, Yoriz, for the trouble taken. As a result of your settings, it looks so much better now. Henceforth, I shall take care in this regard.

adt
(Aug-29-2019, 07:27 AM)adt Wrote: [ -> ]The Link is given above. Overall, the web site appears to be quite helpful.

I am not sure we shall promote the site. In any case I left a note/feedback with suggested improvement. You can check if they will fix the issue and it will tell you actually how good the site is.
(Aug-29-2019, 07:49 AM)DeaD_EyE Wrote: [ -> ]tuple, list, set, dict, ... are builtin types. Their name can be overwritten.
<<- - ->>
The old reference to dict and list is no longer there.

Thanks Dead Eye for the nice clarification. Your demo code does an excellent job of illustrating the point. It also explains the fact that adverse effect is felt in subsequent code snippets rather than the current one.

adt

(Aug-29-2019, 08:05 AM)buran Wrote: [ -> ]You can check if they will fix the issue and it will tell you actually how good the site is.

OK. Thanks again for your kind initiative.

adt