![]() |
how can I display only desired items? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how can I display only desired items? (/thread-35868.html) |
how can I display only desired items? - 3lnyn0 - Dec-24-2021 How can I write the code for the 'predator' name entered from the keyboard to be something like this (without the name appearing in the output, only the ones below): id -> 1 producer - acer category -> laptop price -> 1000 stock -> 5 products = ['id', 'name', 'producer','category','price', 'stock'] list1 = [ { "id": "1", "name": "predator", "producer": "acer", "category": "laptop", "price": 1000, "stock": 5 }, { "id": "2", "name": "monitor1", "producer": "dell", "category": "monitor", "price": 500, "stock": 10 } ] product_name = input('Enter the name of product: ') for el in val: if el['name'] == product_name: del el['name'] print(el) RE: how can I display only desired items? - BashBedlam - Dec-24-2021 Is this what you're looking for? products = ['id', 'name', 'producer','category','price', 'stock'] list1 = [ { "id": "1", "name": "predator", "producer": "acer", "category": "laptop", "price": 1000, "stock": 5 }, { "id": "2", "name": "monitor1", "producer": "dell", "category": "monitor", "price": 500, "stock": 10 } ] product_name = input('Enter the name of product: ') for el in list1: if el ['name'] == product_name : for key, value in el.items () : if key != 'name' : print (key, value) RE: how can I display only desired items? - 3lnyn0 - Dec-24-2021 (Dec-24-2021, 10:01 PM)BashBedlam Wrote: Is this what you're looking for? yes, it's perfect thanks! RE: how can I display only desired items? - menator01 - Dec-24-2021 Another way #! /usr/bin/env python3 mydict = { 'predator': { 'id': 1, 'name': 'predator', 'producer': 'acer', 'category': 'laptop', 'price': 1000, 'stock': 5 }, 'monitor': { 'id': 2, 'name': 'monitor1', 'producer': 'dell', 'category': 'monitor', 'price': 500, 'stock': 10 } } product_name = input('Enter name of product: ').lower() if product_name in mydict.keys(): for key, value in mydict.items(): if key == product_name: for item, description in value.items(): print(f'{item}: {description}') else: print('No product with that name')
RE: how can I display only desired items? - ndc85430 - Dec-25-2021 Why the unnecessary loop on line 25? Dicts let you access items by key, so mydict[product_name] gets you the value associated with key product_name .
RE: how can I display only desired items? - menator01 - Dec-25-2021 (Dec-25-2021, 03:38 PM)ndc85430 Wrote: Why the unnecessary loop on line 25? Dicts let you access items by key, so You are right. #! /usr/bin/env python3 mydict = { 'predator': { 'id': 1, 'name': 'predator', 'producer': 'acer', 'category': 'laptop', 'price': 1000, 'stock': 5 }, 'monitor': { 'id': 2, 'name': 'monitor1', 'producer': 'dell', 'category': 'monitor', 'price': 500, 'stock': 10 } } product_name = input('Enter name of product: ').lower() if product_name in mydict.keys(): print() text = f'Details for {product_name.title()}' widths = [] for n in mydict[product_name].keys(): widths.append(len(n)) width = max(widths) print(text) print(f'-' * len(text)) print() for item, desc in mydict[product_name].items(): if len(item) < width: space = (width - len(item)+1) else: space = 1 print(f'{item.upper()}: {" " * space}{desc}') else: print('No product with that name')
|