Am i new learner for python, can not understand how to build loop

question: * Print the product_id on one line

products = [
{
“discontinued”: 0,
“lead_time_days”: 4,
“product_category”: “Toy”,
“product_description”: “Pull out a bock without crashing the stack …”,
“product_id”: 101,
“product_name”: “Jenga Classic Game”,
“reorder_level”: 50,
“unit_price”: 14.99
},
{
“discontinued”: 0,
“lead_time_days”: 4,
“product_category”: “Wireless Phone Accessory”,
“product_description”: “Display: 2.5 inches Camera: 2 MP Talk Time: 4.5 hours Weight: 3.3 ounces”,
“product_id”: 102,
“product_name”: “AT&T Z431 GoPhone (AT&T)”,
“reorder_level”: 14,
“unit_price”: 49.99
},
{
“discontinued”: 1,
“lead_time_days”: 4,
“product_category”: “Wireless Phone Accessory”,
“product_description”: “Display: 4.5-inches Camera: 5-MP Input: Touchscreen OS: Android”,
“product_id”: 103,
“product_name”: “AT&T Z998 LTE Android Go Phone (AT&T Prepaid)”,
“reorder_level”: 29,
“unit_price”: 159.99
},
{
“discontinued”: 1,
“lead_time_days”: 4,
“product_category”: “Personal Computers”,
“product_description”: “8 inch Display (1920x1200) …”,
“product_id”: 104,
“product_name”: “NVIDIA SHIELD Tablet (WiFi)”,
“reorder_level”: 10,
“unit_price”: 299.0
}
]
how can i write this one code

Welcome to Python Discuss!

First, I would recommend using proper code blocks rather than pasting them as plaintext:
```python3
code here
```

Also, it looks like your quotation marks are rather than " or ', which leads to issues when attempting to copy the code. Here’s how I would format it:

products = [
{
'discontinued': 0,
'lead_time_days': 4,
'product_category': 'Toy',
'product_description': 'Pull out a bock without crashing the stack …',
'product_id': 101,
'product_name': 'Jenga Classic Game',
'reorder_level': 50,
'unit_price': 14.99
},
{
'discontinued': 0,
'lead_time_days': 4,
'product_category': 'Wireless Phone Accessory',
'product_description': 'Display: 2.5 inches Camera: 2 MP Talk Time: 4.5 hours Weight: 3.3 ounces',
'product_id': 102,
'product_name': 'AT&T Z431 GoPhone (AT&T)',
'reorder_level': 14,
'unit_price': 49.99
},
{
'discontinued': 1,
'lead_time_days': 4,
'product_category': 'Wireless Phone Accessory',
'product_description': 'Display: 4.5-inches Camera: 5-MP Input: Touchscreen OS: Android',
'product_id': 103,
'product_name': 'AT&T Z998 LTE Android Go Phone (AT&T Prepaid)',
'reorder_level': 29,
'unit_price': 159.99
},
{
'discontinued': 1,
'lead_time_days': 4,
'product_category': 'Personal Computers',
'product_description': '8 inch Display (1920x1200) …',
'product_id': 104,
'product_name': 'NVIDIA SHIELD Tablet (WiFi)',
'reorder_level': 10,
'unit_price': 299.0
}
]

The indentation can help as a visual aid, but it’s not required and can be a bit annoying to add properly on Discuss.

As for your question, exactly what you were looking for was not entirely clear to me. By “print the product_id on one line” do you mean printing all of the product_ids from each product on a single line?

If so, that could be done rather simply with something like this:

for product in products:
    print(product['product_id'], end=', ')

Result:

101, 102, 103, 104,

However, a better option would probably be to build a list using the product IDs from each entity, and then print out a string containing the elements in the list. This would be far more reusable, and remove the trailing comma:

product_ids = [product['product_id'] for product in products]
""" This can also be represented as:
product_ids = []
for product in products:
    product_ids.append(product['product_id'])
"""
print(", ".join(str(id) for id in product_ids)) #convert ints to strings so that we can join using commas

Result:

'101, 102, 103, 104'

Let me know if you have any questions or if you were looking for a different answer.

Edit: Fixed typo, was missing in for str(id) for id in product_ids

thank so much thanks

No problem! Let me know if you have any further questions about any part of what I did, such as looking up individual attributes, list comprehension, etc.