Python Container Types and Handy Tricks for Lists, Dictionaries, and More
This article presents a collection of practical Python techniques for working with built‑in container types such as lists, tuples, sets, and dictionaries, covering operations like extracting maximum values, counting elements, slicing, copying, mapping, filtering, column extraction, and transposition, all illustrated with clear code examples.
Python provides a rich set of container data types, including list , tuple , set , and dict . Below are several useful tricks to write more Pythonic code.
Extract maximum value from a dictionary
<code>max(my_dict.values())</code>Get the key of the maximum value:
<code>max(my_dict, key=my_dict.get)</code>Get both the key and value of the maximum entry:
<code>max(my_dict.items(), key=lambda x: x[1])</code>Alternative using operator.itemgetter :
<code>import operator
max(my_dict.items(), key=operator.itemgetter(1))</code>Count occurrences of each element in a list
<code>{x: my_list.count(x) for x in set(my_list)}</code>Or using itertools.groupby :
<code>from itertools import groupby
{key: len(list(group)) for key, group in groupby(sorted(my_list))}</code>Or using collections.Counter :
<code>from collections import Counter
dict(Counter(my_list))</code>Truncate a list efficiently
<code>my_list = my_list[:i]
my_list = my_list[j:]</code>Better approach using del :
<code>del my_list[i:]
del my_list[:j]</code>Zip operation with the longest iterable
<code>list(zip('abc', [1, 2, 3, 4]))</code>Result contains elements up to the shortest iterable. To use the longest iterable, employ itertools.zip_longest :
<code>from itertools import zip_longest
list(zip_longest('abc', [1, 2, 3, 4]))</code>Copying lists
Shallow copy via slicing:
<code>thy_list = my_list[:]</code>Deep copy using copy.deepcopy :
<code>import copy
thy_list = copy.deepcopy(my_list)</code>Apply a function to multiple iterables with map
<code>my_list = [11, 13, 15, 17]
thy_list = [2, 4, 6, 8, 10]
list(map(lambda x, y: x + y, my_list, thy_list))</code>Equivalent using list comprehension and zip :
<code>my_list = [11, 13, 15, 17]
thy_list = [2, 4, 6, 8, 10]
[x + y for x, y in zip(my_list, thy_list)]</code>Remove falsy values (None, 0, empty) from a list
<code>list(filter(bool, my_list))</code>Or using a list comprehension:
<code>[x for x in my_list if x]</code>Extract a specific column from a nested list (matrix)
<code>my_list = [
[1, 1, 2, 2],
[5, 6, 7, 8],
[3, 3, 4, 4],
]
col1, *_ = zip(*my_list)
list(col1) # => [1, 5, 3]</code>Similarly, to get the second column:
<code>_, col2, *_ = zip(*my_list)
list(col2) # => [1, 6, 3]</code>Transpose a matrix
<code>[list(x) for x in zip(*my_list)]</code>The above yields the transposed matrix as a list of rows.
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.