From c744117eff29238b1643abb2c1c9339bb1c4a8fb Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 21 Mar 2026 12:33:09 -0500 Subject: [PATCH] Add Deduplicate A List Into A Tuple as a Python TIL --- README.md | 3 +- python/deduplicate-a-list-into-a-tuple.md | 46 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 python/deduplicate-a-list-into-a-tuple.md diff --git a/README.md b/README.md index 4f15b6a..bf8d3b2 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1761 TILs and counting..._ +_1762 TILs and counting..._ See some of the other learning resources I work on: @@ -1048,6 +1048,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Control Passing Of Time In Tests](python/control-passing-of-time-in-tests.md) - [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md) - [Create A Range Of Descending Values](python/create-a-range-of-descending-values.md) +- [Deduplicate A List Into A Tuple](python/deduplicate-a-list-into-a-tuple.md) - [Dunder Methods](python/dunder-methods.md) - [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.md) - [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md) diff --git a/python/deduplicate-a-list-into-a-tuple.md b/python/deduplicate-a-list-into-a-tuple.md new file mode 100644 index 0000000..ac7e6f7 --- /dev/null +++ b/python/deduplicate-a-list-into-a-tuple.md @@ -0,0 +1,46 @@ +# Deduplicate A List Into A Tuple + +A `list` is not hashable which means you can't use it for things like `dict` +keys or cache keys. Instead you need to convert it into something like a `set` +or a `tuple`. + +Here is an example list: + +```python +>>> l1 = [3,4,1,2,5,4,1] +``` + +Turning this list into a `set` or `frozenset` is straightforward: + +```python +>>> set(l1) +{1, 2, 3, 4, 5} +>>> frozenset(l1) +frozenset({1, 2, 3, 4, 5}) +``` + +If you're trying to preserve the order after deduplicating, then you'll want to +use a `tuple` instead of a `set`. In order to deduplicate while maintaining the +ordering, you can exploit the fact that `dict` keys maintain their order. A +`list` can be transformed into the keys of a `dict` with +[`dict.fromkeys`](https://docs.python.org/3/library/stdtypes.html#dict.fromkeys): + +```python +>>> dict.fromkeys(l1) +{3: None, 4: None, 1: None, 2: None, 5: None} +``` + +And here is your `tuple` which extracts the keys of the `dict`: + +```python +>>> tuple(dict.fromkeys(l1)) +(3, 4, 1, 2, 5) +``` + +By comparison, here is the `tuple` transformed directly from the `list` without +deduplication. + +```python +>>> tuple(l1) +(3, 4, 1, 2, 5, 4, 1) +```