1

I have tried to execute the following basic statements in a python console and what I got is an error saying:

dict object is  not  callable 

The code I executed:

>>> test_dict = {1:"one",2:"two"}
>>> set3= set(test_dict)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'dict' object is not callable

I have gone through some questions on web but couldn't find and understand anything till now.

  • A dict has key-value _pairs_ ... what items do you expect to show up in your set? – Paul Becotte Dec 10 '15 at 14:16
  • 4
    You are not telling us everything. Your code does not raise an exception by itself. – zvone Dec 10 '15 at 14:16
  • @PaulBecotte By default, keys will be used whenever a dictionary is iterated. – thefourtheye Dec 10 '15 at 14:17
  • `set(test_dict)` gives the keys, ie: `set([1, 2])`. But if above in your code you declared something like `set = {...}`, you only could be guilty for it. BTW **never** use Python standard names like list, set or dict for you own variables. – Serge Ballesta Dec 10 '15 at 14:25

3 Answers3

2

You can construct a set from a dictionary; the set will be initialized to the set of keys in the dictionary.

However, in your case, the name set has been bound to a dict value, so when you write set, you don't get the built-in set class, but that dictionary. Write set = __builtins__.set to restore it in an interactive shell. In a program, search for set = (or as set) in the code before.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • @avinash625 Please mention how, so that other people can benefit from your solution! If this or any other answer solved it for, please consider [accepting it](http://meta.stackexchange.com/a/5235/141962). If the problem was something else, please write your own answer (and accept it later). Thanks! – phihag Dec 10 '15 at 14:29
  • 1
    searched for the set = {} and deleted the binding that i created and then it ran fine – not_so_sure Dec 10 '15 at 15:16
1

You are masking the built-in set by assignment.

>>> set = {}

This makes the set name point to a new dictionary object, you can no longer use it as the built in type that creates new set objects:

>>> test_dict = {1:"one", 2:"two"}
>>> set3 = set(test_dict)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable

Don't mask the built-in names, simply delete the name binding you created and now everything will run fine:

>>> del set  # 'undos' the set={} binding 
>>> set3 = set(test_dict)
>>> set3
{1, 2}
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
1

The code you posted has no problem run under python3 and python2 . If you got this error,it typically means you have reassign set to another object. You should check the code again.

Python 2.7.9 (default, Apr 13 2015, 11:43:15) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test_dict = {1:"one",2:"two"}
>>> set3=set(test_dict)
>>> print set3
set([1, 2])
>>> set3.add(3)
>>> print set3
set([1, 2, 3])
>>> set3.pop()
1

and in python3 :

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test_dict = {1:"one",2:"two"}
>>> set3=set(test_dict)
>>> print(set3)
{1, 2}
>>> set3.add(3)
>>> print(set3)
{1, 2, 3}
>>> set3.pop()
1
>>> print(set3)
{2, 3}
MikeZhang
  • 327
  • 2
  • 10