How to search Relevant Book Id record in list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to search Relevant Book Id record in list?

For example u have one list in whic 5 book records are saved (each book have unique id number, name,author name,price) Means 20 values are stored in list Lets suppose one of the book have 3 unique id i want to search this id nd show all Related Information to that book ? How can i do tht

9th May 2019, 4:29 PM
Abdul Wahab
Abdul Wahab - avatar
8 Answers
+ 4
hi, sounds a little bit confusing to me. You mentioned that each book has a unique id number. And then you say ā€˜... one of the book have 3 unique id ...ā€™ Can you explaine it again? And the other information that is missing is the programming language you want to use.
9th May 2019, 4:55 PM
Lothar
Lothar - avatar
+ 4
hi, now itā€™s clear for me. I would suggest to store the data in a dict. This structure does always consists of a key: value pair. Key must be unique, value can contain data in our case a list with the detail data. sample: books = { '1': ['Riverboat', 'Sam Gold', 22.50], ....} Then you can search with the dict method .get for an id and print the detail data.
9th May 2019, 5:55 PM
Lothar
Lothar - avatar
+ 2
hi Abdul, here my try inspired from your code https://code.sololearn.com/c8NrSX9s71Z2/?ref=app
10th May 2019, 4:14 PM
Lothar
Lothar - avatar
+ 1
Don't use a list. If you need to search, given an unique key, dictionaries are the better data structures.
9th May 2019, 9:27 PM
Daniel Adam
Daniel Adam - avatar
+ 1
Daniel Adam Lothar thanks both of u i have completed my project thanksballot
9th May 2019, 9:38 PM
Abdul Wahab
Abdul Wahab - avatar
+ 1
Lothar - clean code, but ... :D A) If you use a list and your id is same as the index, use it to access the item directly! That results in O(1), so it's constant. B) If that's not the case, use a dictionary so accessing is O(log n) with n the number of elements in your list Otherwise your algorithm has O(n) and that's ... Not so good for large n. Example: log(1.000.000) = 6, compared to 1.000.000 ... is just a little better.
10th May 2019, 7:12 PM
Daniel Adam
Daniel Adam - avatar
0
Sir let suppose on book unique id is "3" I want to search the book whose id is "3" Nd then it will print all the information of tht book Like Book id 3 Name aaaa Price 80 AutName Khan The Language Is python
9th May 2019, 5:20 PM
Abdul Wahab
Abdul Wahab - avatar
0
Thanks Allot
31st May 2019, 11:21 AM
Abdul Wahab
Abdul Wahab - avatar