Using Lists and Cases

Python Using Lists and Cases: Learn Python Lists & Cases with Real Examples
Written by Paayi Tech |19-Apr-2019 | 0 Comments | 402 Views

Using List as Stack:

A stack is another type of data structure. Python doesn’t offer a separate module to operate stack, but it can be used by using a simple python list. Stack works on a concept of “First in Last Out.” That means the element that is inserted first in the list will be extracted in last. The list built-in function makes it very easy to push the data and pops the data out from the list. Let us take a look at how we can use the list as a stack in python.


ls = []
print (ls.pop ()) #4
print (ls.pop ()) #3
print (ls.pop ()) #2
print (ls.pop ()) #1
print (ls.pop ()) #Empty List error


In the above-given example, we first initialize the empty list. Then we append 1 than 2 than 3 and in last 4. However, when we populate data the data that we inserted last will pop out first.


Use Cases of Stack:

Now the question is what the practical application of using the stack in real life programming is. In many applications we see there are back buttons, an application like websites and mobile application have these functionalities. It works by using the stack. On each successive page load, the page URLs are appended in a stack, and when we press the back button, it will pop out from the stacks.

A similar operation is done on using breadcrumbs in web development. Moreover, stacks are also used to schedule the functions in programming.


Using List as Queues:

Queues are another type of data structure. Similar to stack there is no such separate module to operate queues. Here also lists are used to make queues functions. Implementation of the queue is a little different from stacks. Queues have two main functionalities:

  • enqueue
  • dequeue

Enqueue insert the element in a queue and dequeue remove the element from the list. It is based on first in first out. For example, there is a long queue of cars at the petrol pump the car that came first will leave first. Similar functionality is in a queue data structure. To enqueue the element normally, we use the append function as we have seen earlier in lists and stack data structure but to dequeue the element, we import the dequeue function from the module collection which is a built-in module in all python which allows us to remove the element that is inserted first.


Following is the way to use the list as queues in python:

from collections import deque
queue_list = deque(["Car1","Car2","Car3","Car4"])
print(queue_list.popleft()) #Car1
print(queue_list.popleft()) #Car2
print(queue_list.popleft()) #Car3

In the above-given example, we first apply the module dequeue to the list. Which will convert the list function to queue data structure. This functionality will provide additional functionality that pop left which will throw the element that is inserted first.


Use Cases:

A queue data structure is vastly used in programming. The queue data structure is greatly used in async tasks. In async task functions are call in queues and when they are fully executed their output is extracted and pop out and remaining function will then be call, allowing to execute code without blocking the code.

Login/Sign Up


Related Posts

© Copyright 2020, All Rights Reserved.

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.