Python Tips and Tricks: Variable Swapping, Comprehensions, Counter, JSON Pretty‑Print, FizzBuzz, and More
This article presents a collection of practical Python techniques—including one‑line variable swapping, list, set and dictionary comprehensions, the Counter class for counting, pretty‑printing JSON, inline conditional expressions, list slicing, itertools combinations, and a quirky False‑equals‑True example—each illustrated with concise code snippets and explanations.
Python’s concise syntax enables many powerful tricks that simplify everyday programming tasks. The article begins by showing how to swap two variables without a temporary placeholder using the one‑liner a, b = b, a , followed by a quick demonstration of printing the swapped values.
It then introduces list, set, and dictionary comprehensions, illustrating how to generate a new list with another_list = [x + 1 for x in some_list] , create a set of even numbers with even_set = {x for x in some_list if x % 2 == 0} , and build a boolean mapping dictionary with d = {x: x % 2 == 0 for x in range(1, 11)} . The same syntax can also produce a literal set directly: my_set = {1, 2, 3, 4} .
The collections.Counter class is highlighted for counting occurrences in a string: c = Counter('hello world') and retrieving the most common items with c.most_common(2) . The article also covers pretty‑printing JSON using the built‑in json module, showing both compact and indented output via json.dumps(data, indent=2) .
Inline conditional expressions are demonstrated with print('Hello' if True else 'World') . List concatenation and string formatting examples combine two football‑team lists, and the + operator is used to join strings and numbers.
Numeric comparisons can be chained in Python, as shown by if 3 > x > 1: print(x) . Iterating over two lists simultaneously is achieved with for a, b in zip(nfc, afc): print(a + ' vs. ' + b) , while enumerate provides index‑value pairs for a list.
Classic list comprehension for filtering even numbers is transformed into a more compact form: even = [n for n in numbers if n % 2 == 0] . A dictionary comprehension that swaps keys and values from an enumerated list is shown as {key: value for value, key in enumerate(teams)} .
Other useful snippets include initializing a list with repeated values ( items = [0] * 3 ), converting a list to a comma‑separated string ( ', '.join(teams) ), safely retrieving dictionary values with data.get('admin', False) , and various list‑slicing techniques ( x[:3] , x[1:5] , x[::2] , etc.).
Additional examples cover the itertools.combinations function for generating pairwise combinations of a list, and a playful demonstration that reassigns False = True and uses an if statement to print "Hello".
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.