Fundamentals 8 min read

Finding Lucky Numbers in a List Using Python: Step‑by‑Step with map, zip, filter and a One‑Liner

This tutorial explains how to solve the LeetCode "Lucky Numbers in a List" problem in Python by extracting unique elements, counting their occurrences with map and count, pairing them with zip, filtering with lambda, sorting the result, and finally compressing the whole logic into a single expressive line of code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Finding Lucky Numbers in a List Using Python: Step‑by‑Step with map, zip, filter and a One‑Liner

The article introduces a LeetCode problem called "Lucky Numbers in a List" where a number is considered lucky if its frequency in the list equals the number itself (e.g., in [1, 2, 2, 3] the lucky numbers are 1 and 2).

Step 1 – Get unique elements : The simplest way to remove duplicates is to convert the list to a set. Example:

arr = [3,5,2,7,3,8,1,2,4,8,9,3]
unique = set(arr)
print(unique)  # {1, 2, 3, 4, 5, 7, 8, 9}

Step 2 – Count occurrences : Each element’s count can be obtained with the list method count(). The article first shows a manual loop, then replaces the loop with map to produce a generator of counts.

<code>pairs = []
for i in unique:
    pairs.append((i, arr.count(i)))
print(pairs)  # [(1, 1), (2, 2), (3, 3), (4, 1), (5, 1), (7, 1), (8, 2), (9, 1)]

m = map(arr.count, unique)          # generator of counts
z = zip(unique, m)                  # pairs each element with its count

Step 3 – Filter lucky numbers : Instead of iterating, the filter function together with a lambda expression selects only those pairs where the element equals its count. The result is then sorted.

f = filter(lambda x: x[0] == x[1], z)
s = sorted(f, key=lambda x: x[0])
print('Lucky numbers are:', [item[0] for item in s])
# Output: Lucky numbers are: [1, 2, 3]

One‑liner version : All the steps can be collapsed into a single expression that directly prints the lucky numbers.

print('Lucky numbers are:', [item[0] for item in sorted(filter(lambda x: x[0]==x[1], zip(set(arr), map(arr.count, set(arr)))), key=lambda x:x[0])])
# Output: Lucky numbers are: [1, 2, 3]

The author reflects that while the one‑liner looks clever, a more readable multi‑step solution is often preferable for clarity.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmPythonLambdaMAPListfilterlucky numbers
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.