Python Forum
how can I display only desired items?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I display only desired items?
#1
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)
Reply
#2
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)
3lnyn0 likes this post
Reply
#3
(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!
Reply
#4
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
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.
Reply
#6
(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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 45,703 May-06-2023, 08:14 AM
Last Post: pramod08728
  Json filter is not capturing desired key/element mrapple2020 1 1,073 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  Not rounding to desired decimal places? pprod 2 2,505 Mar-05-2021, 11:11 AM
Last Post: pprod
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,172 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Why this code not getting desired output ? MDRI 2 2,485 Sep-18-2020, 02:11 AM
Last Post: MDRI
  showing only desired part of a plot grknkilicaslan 1 2,291 Jul-10-2020, 03:51 PM
Last Post: Marbelous
  [Beginner] Code is not producing desired result fakej171 2 2,380 Mar-21-2020, 10:26 AM
Last Post: buran
  Not Getting the Desired value using json.dumps saurabh210 0 1,470 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  Code isn't working in several IDE's when other people are getting desired output. SuperSymmetry 1 2,527 Jul-22-2017, 07:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020