1
0
mirror of https://github.com/jbranchaud/til synced 2026-07-04 08:38:23 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
jbranchaud eb0a7e1b3d Link to the VisualMode blog from the README 2026-03-21 13:28:25 -05:00
jbranchaud c1cd40311f Add Browse And Search Help Docs as a Unix TIL 2026-03-21 12:34:49 -05:00
jbranchaud c744117eff Add Deduplicate A List Into A Tuple as a Python TIL 2026-03-21 12:33:09 -05:00
3 changed files with 72 additions and 1 deletions
+4 -1
View File
@@ -10,10 +10,11 @@ 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..._
_1763 TILs and counting..._
See some of the other learning resources I work on:
- [The VisualMode Blog](https://visualmode.dev/blog)
- [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7)
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
- [Vim Un-Alphabet](https://www.youtube.com/playlist?list=PL46-cKSxMYYCMpzXo6p0Cof8hJInYgohU)
@@ -1048,6 +1049,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)
@@ -1648,6 +1650,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [All The Environment Variables](unix/all-the-environment-variables.md)
- [Apply Successive Filters To Lines In Less](unix/apply-successive-filters-to-lines-in-less.md)
- [Authorize A cURL Request](unix/authorize-a-curl-request.md)
- [Browse And Search Help Docs](unix/browse-and-search-help-docs.md)
- [Cat A File With Line Numbers](unix/cat-a-file-with-line-numbers.md)
- [Cat Files With Color Using Bat](unix/cat-files-with-color-using-bat.md)
- [Change Default Shell For A User](unix/change-default-shell-for-a-user.md)
+46
View File
@@ -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)
```
+22
View File
@@ -0,0 +1,22 @@
# Browse And Search Help Docs
There are a lot of tools that don't have dedicated `man` pages, but do have
lengthy output when you pass them the `--help` flag.
We can make those details easier to browse and searchable by piping them to
`less`.
```bash
uv run pytest --help | less
```
First, we see the top of the output inside `less` instead of bottom of the
output right above our next terminal prompt.
From `less`, we can use down and up arrows (or `j` and `k`) to navigate through
the details. We can also jump to a specific word or phrase by searching -- type
`/` and then the pattern we're trying to match. `n` and `N` to go to the next or
previous match, respectively.
See `man less` more more details. And if you like these improvements to viewing
tool usage details, you may also be interested in [a better man page viewer](https://www.visualmode.dev/a-better-man-page-viewer).