Python Forum
Sub: Python-3: Better Avoid Naming A Variable As list ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sub: Python-3: Better Avoid Naming A Variable As list ?
#1
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
A.D.Tejpal
Reply
#2
That apply not only to list but to all built in functions, modules, packages, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
Thanks buran, Thomast - for your kind inputs.

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

The Link is given above. Overall, the web site appears to be quite helpful.
A.D.Tejpal
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(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
A.D.Tejpal
Reply
#9
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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
A.D.Tejpal
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string using variable found in a list japo85 2 1,306 Jul-11-2022, 08:52 AM
Last Post: japo85
  naming entities(city abbreviations) tirumalaramakrishna 1 1,250 May-06-2022, 11:22 AM
Last Post: jefsummers
  Avoid third party functions to wrote my python code into system debug-log? mark 9 2,217 Apr-09-2022, 08:41 PM
Last Post: mark
  An IF statement with a List variable dedesssse 3 8,298 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Python Style and Naming Variables rsherry8 3 2,226 Jun-07-2021, 09:30 PM
Last Post: deanhystad
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,660 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  Create variable and list dynamically quest_ 12 4,438 Jan-26-2021, 07:14 PM
Last Post: quest_
  Naming the file as time and date. BettyTurnips 3 2,991 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
Question Matching variable to a list index Gilush 17 5,914 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  Python linter Pylance: What does "(variable): List | Unbound" mean? Drone4four 1 6,303 Oct-23-2020, 08:31 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020