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:
Esteemed members might like to look into it & offer their views.
adt
""" 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