Python Forum

Full Version: how can I display only desired items?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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)
(Dec-24-2021, 10:01 PM)BashBedlam Wrote: [ -> ]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)

yes, it's perfect

thanks!
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')
Output:
Enter name of product: monitor id: 2 name: monitor1 producer: dell category: monitor price: 500 stock: 10
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.
(Dec-25-2021, 03:38 PM)ndc85430 Wrote: [ -> ]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.

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')
Output:
Enter name of product: predator Details for Predator -------------------- ID: 1 NAME: predator PRODUCER: acer CATEGORY: laptop PRICE: 1000 STOCK: 5