[Book Name, Author, Price] 

Each of these records are nested together to form a nested list. Write the following user defined functions in Python to perform the specified operations on the stack named books.

(i) Push_element(BList): It takes the nested list as an argument and pushes a list object containing book name and author, which have price less than 300.

Answer:

Answer by student

[SQP] (i) Push_element(BList): It takes the nested list as an argument - Teachoo Sample Paper - Computer Science Class 12

Detailed answer by teachoo

-lock-

The code defines a function named Push_element that takes one parameter BList, which is a nested list of book details . Each sublist in BList contains three elements: book name, author, and price. The function performs the following steps:

    • It loops through each sublist in BList using a for loop . In each iteration, it assigns the current sublist to a variable called sublist.
    • It checks if the price element of the sublist (which is at index 2) is less than 300 using an if statement. If the condition is True, it means that the book has a low price and should be pushed to the stack.
    • It pushes a list object containing the book name and author elements of the sublist (which are at index 0 and 1 respectively) to the stack using the append method. This adds the list object to the end of the books list, which represents the top of the stack.

The final code is:

part 2 - Question 30 (i) - Teachoo Sample Paper - Computer Science Class 12 - Solutions to CBSE Sample Paper - Computer Science Class 12 - Computer Science - Class 12

-endlock-

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.