Fundamentals 9 min read

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

This article explains how to identify "lucky numbers"—elements whose value equals their frequency—in a list by progressively applying Python fundamentals such as set for deduplication, count, map, zip, filter, lambda and sorted, culminating in a concise one‑liner solution.

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, filter, zip and lambda

While browsing LeetCode the author encountered a small but intriguing problem called "Lucky Numbers in a List" and used it to showcase many Python programming techniques, including sets, map, zip, lambda, filter, list comprehensions and sorting.

A lucky number is defined as an integer whose occurrence count in the list equals the integer itself. For example, in [1, 2, 2, 3] the numbers 1 and 2 are lucky because they appear 1 and 2 times respectively.

The target list for the demonstration is [3, 5, 2, 7, 3, 8, 1, 2, 4, 8, 9, 3]. The solution is broken into three logical steps.

Step 1 – Remove duplicates using a set:

>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]<br>>> unique = set(arr)<br>>> unique<br>{1, 2, 3, 4, 5, 7, 8, 9}

Step 2 – Count occurrences . The list method count() returns the frequency of an element:

>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]<br>>> arr.count(8)  # element 8 appears twice<br>2

Building a list of (value, count) pairs can be done with a simple loop:

>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]<br>>> unique = set(arr)  # remove duplicates<br>>> pairs = list()  # store tuples (value, count)<br>>> for i in unique:
    pairs.append((i, arr.count(i)))<br>>> pairs<br>[(1, 1), (2, 2), (3, 3), (4, 1), (5, 1), (7, 1), (8, 2), (9, 1)]

To avoid the explicit loop, map() can generate the counts as a generator:

>> m = map(arr.count, unique)<br>>> m<br><map object at 0x0000020A2D090E08><br>>> list(m)  # convert to list for inspection<br>[1, 2, 3, 1, 1, 1, 2, 1]<br>>> list(m)  # generators are exhausted after one use<br>[]

Similarly, zip() pairs each unique element with its count, also returning a generator:

>> m = map(arr.count, unique)<br>>> z = zip(unique, m)<br>>> z<br><zip object at 0x0000020A2D490508><br>>> list(z)
[(1, 1), (2, 2), (3, 3), (4, 1), (5, 1), (7, 1), (8, 2), (9, 1)]<br>>> list(z)
[]

Step 3 – Filter lucky numbers . A custom function can be replaced by a lambda inside filter(), and the result can be sorted:

>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]<br>>> unique = set(arr)<br>>> m = map(arr.count, unique)<br>>> z = zip(unique, m)<br>>> f = filter(lambda x: x[0] == x[1], z)<br>>> s = sorted(f, key=lambda x: x[0])<br>>> print('Lucky numbers are:', [item[0] for item in s])
Lucky numbers are: [1, 2, 3]

The entire logic can be collapsed into a single line:

>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> 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])])
Lucky numbers are: [1, 2, 3]

For readability, a straightforward list comprehension also works:

>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> [x for x in set(arr) if x == arr.count(x)]
[1, 2, 3]

Through this example the reader sees how basic Python constructs can be combined and then progressively refactored into more compact, functional‑style code while still remaining understandable.

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.

algorithmPythonLambdaLeetCodeMAPList
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.