How to Find the Minimum Value in a List in Python
Use the built-in min() to get the smallest item of a Python list, and learn how to handle indexes, empty lists, mixed types and custom comparisons.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Use the built-in min() to get the smallest item of a Python list, and learn how to handle indexes, empty lists, mixed types and custom comparisons.
Use max(my_list) to get the largest item in a Python list. Learn the key and default arguments, how to handle empty lists and how to find the index.
Use len() to count the key-value pairs in a Python dictionary, and learn how to handle nested dictionaries, empty checks and conditional counts.
Use len(my_tuple) to get the number of elements in a Python tuple. It is constant time, and a one-element tuple needs a trailing comma to count as a tuple.
Use len(my_list) to get the number of items in a Python list. It runs in constant time and counts only top-level elements, including for nested lists.
Use len(text) to get the number of characters in a Python string. It counts Unicode code points in constant time, including spaces and punctuation.
Tuples are immutable, so reversing returns a new tuple. Use slicing or tuple(reversed(t)), and see how nested and named tuples behave.
Reverse a Python list in place with reverse(), or make a reversed copy with slicing or reversed(). Compare the methods, memory use and common mistakes.
The idiomatic way to reverse a Python string is text[::-1]. Compare it with reversed() and join(), loops and recursion, and learn the Unicode pitfalls.
Use tuple(d), tuple(d.values()) or tuple(d.items()) to snapshot a dictionary. See hashable tuple keys, unpacking and named tuples.
Wrap keys(), values() or items() in list() to turn a dictionary into a list, then reshape or sort it with comprehensions and sorted().
Use str() for debugging and json.dumps() for valid JSON. See how to pretty-print, handle dates and convert the string back to a dictionary.
Learn the main ways to turn a Python tuple into a dictionary: dict() on pairs, zip() for two tuples, slicing for flat tuples, and comprehensions.
Use list() to turn an immutable tuple into a mutable list. This guide also covers unpacking, comprehensions and converting nested tuples.
Learn how to turn a Python tuple into a string with str(), join() and f-strings, including tuples of numbers and nested tuples.
Turn parallel lists, pair lists or records into dictionaries with zip(), dict() and comprehensions, and avoid the common pitfalls.
Pass the list to tuple() to get an immutable copy. This guide covers shallow versus deep conversion, hashable keys and converting a tuple back to a list.
Use separator.join(items) to turn a list into a string, and map(str, items) when the elements are not strings. Compare it with str(), print and json.dumps.
Use json.loads() for JSON text and ast.literal_eval() for Python dict literals. Avoid eval(), and handle parse errors with a safe helper function.
Use tuple(text) for characters, tuple(text.split()) for words and ast.literal_eval() for tuple-like strings. Includes unpacking and one-element tuple pitfalls.