[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
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:
-endlock-