Week #47 - 2022

Week #47 - 2022


1 min read

You should beware when counting the length of a string in Python. As shown below, getting a string's length might give a surprising result.

>>> len("Kraków")
>>> len("🏳️‍🌈")

As perceived by the user, the "Kraków" length should be 6, and the size of the flag icon should be 1. Python counts the length of code points instead of the "visible" character. If you need to count the characters perceived by the users, check out this grapheme library.

If you need to display a map in a Jupyter notebook, you can use the leafmap package.


Unfortunately, the map does not show on Visual Studio Code or DataSpell.

You should not compare two iterators. When you compare two iterators like the following:

# Not using an iterator
>>> nums = [4, 1, 3, 2]
>>> print(sorted(nums) == sorted(nums))

# Using an iterator
>>> rev = reversed(nums)
>>> print(sorted(rev) == sorted(rev))

The reversed function returns an iterator. The first sorted call consumes everything from the rev iterator. The second sorted call will have an empty iterator. So when they are compared, the result is not the same.