Tuesday, 30 June 2026

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

 

Code :

from collections import deque d = deque(maxlen=4) for i in range(6): d.append(i) print(list(d))




Explanation:

๐Ÿ”น 1. Importing deque

from collections import deque

✅ Explanation

deque means Double Ended Queue.

It allows insertion and deletion from both ends efficiently.

Here we'll use a special feature called maxlen.

Think of it like a train with only 4 seats.

Train Capacity = 4

๐Ÿš† [ _ | _ | _ | _ ]

No matter how many passengers come, only 4 passengers can stay.

๐Ÿ”น 2. Creating a Fixed-Size Queue

d = deque(maxlen=4)

✅ Explanation

A deque is created with a maximum capacity of 4.

Current state:

Capacity = 4

[]

Important rule:

If queue becomes full,

new element enters,

oldest element automatically leaves.

Unlike a normal list, you never get an overflow error.

๐Ÿ”น 3. Starting the Loop

for i in range(6):

✅ Explanation

range(6) generates:

0

1

2

3

4

5

Python will execute the loop 6 times.

๐Ÿ”น 4. First Iteration (i = 0)

d.append(0)

Queue before:

[]

Queue after:

[0]

Seats occupied:

๐Ÿš† [0 | _ | _ | _]

Still space available.

๐Ÿ”น 5. Second Iteration (i = 1)

d.append(1)

Queue:

[0,1]

Visual:

๐Ÿš† [0 | 1 | _ | _]

Still not full.

๐Ÿ”น 6. Third Iteration (i = 2)

d.append(2)

Queue:

[0,1,2]

Visual:

๐Ÿš† [0 | 1 | 2 | _]

One seat remains.

๐Ÿ”น 7. Fourth Iteration (i = 3)

d.append(3)

Queue becomes:

[0,1,2,3]

Visual:

๐Ÿš† [0 | 1 | 2 | 3]

Now the queue is completely full.

Capacity:

4 / 4

๐Ÿ”น 8. Fifth Iteration (i = 4)

d.append(4)

Here's the interesting part.

Current queue:

[0,1,2,3]

But there is no empty seat.

So Python automatically removes the oldest element.

Oldest element:

0

After removing 0:

[1,2,3]

Now 4 is inserted.

Final queue:

[1,2,3,4]

Visual:

Before

๐Ÿš† [0 | 1 | 2 | 3]

Passenger 4 arrives

Passenger 0 leaves automatically

๐Ÿš† [1 | 2 | 3 | 4]

This behavior is called a Sliding Window.

๐Ÿ”น 9. Sixth Iteration (i = 5)

d.append(5)

Current queue:

[1,2,3,4]

Again queue is full.

Oldest passenger:

1

Leaves automatically.

New passenger:

5

enters.

Final queue:

[2,3,4,5]

Visual:

Before

๐Ÿš† [1 | 2 | 3 | 4]

Passenger 5 arrives

Passenger 1 leaves

๐Ÿš† [2 | 3 | 4 | 5]

๐Ÿ”น 10. Printing the Queue

print(list(d))

✅ Explanation

The deque is converted into a list.

Final output:

[2, 3, 4, 5]

๐ŸŽฏ Final Output

[2, 3, 4, 5]

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (295) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (11) BI (10) Books (262) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (6) Data Analysis (38) Data Analytics (25) data management (16) Data Science (376) Data Strucures (22) Deep Learning (184) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) 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 (327) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1391) Python Coding Challenge (1176) Python Mathematics (1) Python Mistakes (51) Python Quiz (555) Python Tips (18) 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)