Q.Identify the invalid Python statement from the following: (A) d = dict() (B) e = {} (C) f = [] (D) g = dict{}
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Python provides specific syntax for creating empty dictionaries (dict() or {}) and lists ([] or list()); dict{} is not a valid way to create a dictionary and will result in a syntax error. The invalid statement is (D).
Concept and Intuition
In Python, data structures like lists and dictionaries are fundamental for organizing data. Python offers multiple ways to create these structures, primarily through two methods:
- Constructor functions: Using the type name followed by parentheses, like
list()ordict(). These are functions that return an empty instance of that type. - Literal syntax: Using special symbols that directly represent the structure, like
[]for an empty list or{}for an empty dictionary. This is often more concise and idiomatic Python.
Both methods achieve the same result for creating empty structures, but they are distinct syntactical forms. The key is to use the correct syntax for each method.
Step-by-step Analysis
-
Statement (A):
d = dict()This statement uses the
dict()constructor function to create an empty dictionary. Thedict()function, when called without arguments, returns a new, empty dictionary. This is a perfectly valid and common way to initialize an empty dictionary in Python.After this statement,
dwill be an empty dictionary, equivalent to{}. -
Statement (B):
e = {}This statement uses the literal syntax for creating an empty dictionary. Curly braces
{}are the standard way to define dictionary literals in Python. When empty, they represent an empty dictionary. This is also a perfectly valid and very common, often preferred, way to initialize an empty dictionary.Watch outWhile
{}creates an empty dictionary, if you need an empty set, you must useset(). An empty set cannot be created with{}because{}is reserved for empty dictionaries. For example,type({})is<class 'dict'>, whiletype(set())is<class 'set'>. -
Statement (C):
f = []This statement uses the literal syntax for creating an empty list. Square brackets
[]are the standard way to define list literals in Python. When empty, they represent an empty list. This is a valid and widely used way to initialize an empty list.Alternatively, one could use
f = list(), which also creates an empty list using thelist()constructor. -
Statement (D):
g = dict{}…
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.