I would like to be able my list of dictionaries by key.
However my code is returning the key and the value as follows:
for key in products_dicts[0]:
print(products_dicts[0].keys())
Output:
dict_keys(['{"uniq_id": "b6c0b6bea69c722939585baeac73c13d", "sku": "pp5006380337", "name_title": "Alfred Dunner\\u00ae Essential Pull On Capri Pant", "description": "You\'ll return to our Alfred Dunner pull-on capris again and again when you want an updated, casual look and all the comfort you love. \\u00a0 elastic waistband approx. 19-21\\" inseam slash pockets polyester washable imported \\u00a0 \\u00a0 \\u00a0", "list_price": "41.09", "sale_price": "24.16", "category": "alfred dunner", "category_tree": "jcpenney|women|alfred dunner", "average_product_rating": 2.625, "product_url": "http://www.jcpenney.com/alfred-dunner-essential-pull-on-capri-pant/prod.jump?ppId=pp5006380337&catId=cat1002110079&&_dyncharset=UTF-8&urlState=/women/shop-brands/alfred-dunner/yellow/_/N-gkmp33Z132/cat.jump", "product_image_urls": "http://s7d9.scene7.com/is/image/JCPenney/DP1228201517142050M.tif?hei=380&wid=380&op_usm=.4,.8,0,0&resmode=sharp2&op_usm=1.5,.8,0,0&resmode=sharp", "brand": "Alfred Dunner", "total_number_reviews": 8, "Reviews": [{"User": "fsdv4141", "Review": "You never have to worry about the fit...Alfred Dunner clothing sizes are true to size and fits perfectly. Great value for the money.", "Score": 2}, {"User": "krpz1113", "Review": "Good quality fabric. Perfect fit. Washed very well no iron.", "Score": 4}, {"User": "mbmg3241", "Review": "I do not normally wear pants or capris that have an elastic waist, but I decided to try these since they were on sale and I loved the color. I was very surprised at how comfortable they are and wear really well even wearing all day. I will buy this style again!", "Score": 4}, {"User": "zeqg1222", "Review": "I love these capris! They fit true to size and are so comfortable to wear. I am planning to order more of them.", "Score": 1}, {"User": "nvfn3212", "Review": "This product is very comfortable and the fabric launders very well", "Score": 1}, {"User": "aajh3423", "Review": "I did not like the fabric. It is 100% polyester I thought it was different.I bought one at the store apprx two monts ago, and I thought it was just like it", "Score": 5}, {"User": "usvp2142", "Review": "What a great deal. Beautiful Pants. Its more than I expected.", "Score": 3}, {"User": "yemw3321", "Review": "Alfred Dunner has great pants, good fit and very comfortable", "Score": 1}], "Bought With": ["898e42fe937a33e8ce5e900ca7a4d924", "8c02c262567a2267cd207e35637feb1c", "b62dd54545cdc1a05d8aaa2d25aed996", "0da4c2dcc8cfa0e71200883b00d22b30", "90c46b841e2eeece992c57071387899c"]}\n'])
If I omit the index in my code I get the following error:
AttributeError Traceback (most recent call last)
<ipython-input-12-c723fc75d649> in <module>
2
3 for key in products_dicts[0]:
----> 4 print(products_dicts.keys())
AttributeError: 'list' object has no attribute 'keys'
What is strange about that to me is that if I run this code:.
print(type(reviewers_dicts[0]))
It returns:
<class 'dict'>
Is this a data structure issue?.
CodePudding user response:
Both of your for loops have this format:
for item in things:
print(things.keys())
If your things
are in a list,
things[0].keys()
will be the keys
of the first item. To iterate all the items, use the name you choose in the for loop:
for item in things:
print(item.keys())
# ^^^^ --- not all things, just the current item
CodePudding user response:
Try like this:
listis = [
{
1: "One"
, 11: "OneOne"
}
, {2: "Two"}
, {3: "Three"}
, {4: "Four"}
]
for obj in listis:
for key in obj.keys():
print(key)
You will print all keys of each dictionary in list of dictionaries.