Wednesday, 8 July 2026

Python Coding challenge - Day 1201| What is the output of the following Python Code?

 



Code Explanation:

๐Ÿ”น 1. Importing compress

from itertools import compress
✅ Explanation
compress() is imported from Python's itertools module.
It is used to filter one iterable using another iterable.
It works like a filter mask.

Think of it like a TV remote.

Letters

A   B   C   D

Selectors

1   0   1   0

1 = Keep ✅

0 = Remove ❌

๐Ÿ”น 2. Creating the Data List
letters = ["A", "B", "C", "D"]
✅ Explanation

A list named letters is created.

Current memory:

letters

 │

 ▼

["A","B","C","D"]

These are the values that may be selected.

๐Ÿ”น 3. Creating the Selector List
selectors = [1, 0, 1, 0]
✅ Explanation

Another list named selectors is created.

Current memory:

selectors

 │

 ▼

[1,0,1,0]

Each selector corresponds to one letter.

Relationship:

Letter      A    B    C    D

Selector    1    0    1    0

๐Ÿ”น 4. Calling compress()
compress(letters, selectors)
✅ Explanation

Syntax:

compress(data, selectors)

Python compares both lists position by position.

Rule:

Selector = 1


Keep the value

Selector = 0


Discard the value

๐Ÿ”น 5. First Comparison

Current values:

Letter = A

Selector = 1

Condition:

1 → True

Action:

Keep A ✅

Current result:

[A]

๐Ÿ”น 6. Second Comparison

Current values:

Letter = B

Selector = 0

Condition:

0 → False

Action:

Remove B ❌

Current result:

[A]

๐Ÿ”น 7. Third Comparison

Current values:

Letter = C

Selector = 1

Condition:

1 → True

Action:

Keep C ✅

Current result:

[A, C]

๐Ÿ”น 8. Fourth Comparison

Current values:

Letter = D

Selector = 0

Condition:

0 → False

Action:

Discard D ❌

Final result becomes:

[A, C]

๐Ÿ”น 9. Converting to a List
list(compress(...))
✅ Explanation

compress() returns an iterator.

list() converts it into a normal Python list.

Current value:

["A", "C"]

๐Ÿ”น 10. Printing the Result
print(list(compress(letters, selectors)))
✅ Explanation

Python prints the final filtered list.

Output:

['A', 'C']

๐ŸŽฏ Final Output
['A', 'C']

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (301) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (12) BI (10) Books (274) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (9) Data Analysis (39) Data Analytics (27) data management (16) Data Science (387) Data Strucures (23) Deep Learning (190) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (75) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (340) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1400) Python Coding Challenge (1185) Python Mathematics (4) Python Mistakes (51) Python Quiz (562) Python Tips (22) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)