How do you convert a string to a list in Python?
Use split() for words and delimited fields, list() for characters, and json.loads() for strings that already look like lists. Includes edge cases and examples.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Use split() for words and delimited fields, list() for characters, and json.loads() for strings that already look like lists. Includes edge cases and examples.
Use is None and is not None to test for None in Python. See why identity beats equality, how it differs from falsy checks, and the patterns you will use daily.
Use isinstance(value, set) to check whether a Python variable is a set. Learn when type() differs, how subclasses behave and which mistakes to avoid.
Use isinstance(value, dict) to check for a Python dictionary. Learn when to use type(), Mapping or pattern matching instead.
Use isinstance(value, tuple) to check whether a Python variable is a tuple. Learn when type() differs, how subclasses behave and which mistakes to avoid.
Use isinstance(value, list) to check whether a Python variable is a list. Learn when type() differs, how subclasses behave and which mistakes to avoid.
Use isinstance(value, str) to check whether a Python variable is a string. Covers type(), bytes versus str, StrEnum and a str-or-list pattern.
Use isinstance(value, numbers.Number) to test numeric types and float() inside try/except for strings. Includes the bool, nan and Decimal edge cases.
A for loop iterates over an iterable, while a while loop repeats until a condition fails. Compare use cases, loop control statements and examples.
A list comprehension builds everything in memory; a generator expression yields values lazily. Compare memory, reuse and speed with examples.
A set is mutable, a frozenset is immutable and hashable, so only a frozenset can be a dictionary key or a set element. Compare methods, operators and use cases.
`def` defines full named functions, while `lambda` creates short anonymous ones from one expression. See when to use each.
Python 2 range builds a list while xrange generates numbers lazily. See the memory difference and how Python 3 range replaces both.
break stops the nearest loop, return ends the function and passes a value back. Understand both with runnable Python examples.
pass does nothing and is used where a statement is required, while continue skips the rest of the current loop iteration. Compare them with examples and output.
In Python, try marks code that may fail and except handles the error. Learn matching rules, else, finally and common mistakes with examples.
Python's and requires every operand to be truthy, or needs just one. Learn short-circuiting, returned operands, precedence and common idioms.
Use == to compare values and is to check whether two names point to the same object. Learn the pitfalls with integer caching and None checks.
in tests that a value is present and not in tests that it is absent. See how they behave on each container type, with examples.
del removes by index, slice, key or name, while remove deletes the first matching value. See examples, errors and when to use each.