Home > OS >  Get all fieldnames from the union of two lists
Get all fieldnames from the union of two lists

Time:10-10

I want to get all fieldnames from the union of two lists to later export as csv, but I'm only getting fildname from just one list.

I want to get all fieldnames because when I go to export to csv I get the following error: ValueError: dict contains fields not in fieldnames: 'amzlink', 'url', 'asin'

amazondata = [{'amzlink': 'https://www.amazon.com/dp/B084ZZ7VY3', 'asin': 'B084ZZ7VY3', 'url': 'https://www.amazon.com/s?k=712145360504&s=review-rank'}]

amazonPage = [{'price': '$14.95', 'image': 'https://m.media-amazon.com/images/I/81D1P4QqLfL._AC_SX425_.jpg', 'rating': '4.7 out of 5'}]

result = []

amazonPage.extend(amazondata)
for myDict in amazonPage:
    if myDict not in result:
        result.append(myDict)
print (result[0])

CodePudding user response:

Loop over all the dictionaries, adding the keys to a set.

amazondata = [{'amzlink': 'https://www.amazon.com/dp/B084ZZ7VY3', 'asin': 'B084ZZ7VY3', 'url': 'https://www.amazon.com/s?k=712145360504&s=review-rank'}]

amazonPage = [{'price': '$14.95', 'image': 'https://m.media-amazon.com/images/I/81D1P4QqLfL._AC_SX425_.jpg', 'rating': '4.7 out of 5'}]

result = []

amazonPage.extend(amazondata)
all_fields = set()
for myDict in amazonPage:
    all_fields |= myDict.keys()

print(all_fields)

CodePudding user response:

If you are just looking to get a list of all field names in the dictionaries: Extract the keys from the dictionaries, convert to set, and take union of sets.

Borrowed @Baramr's amazondata list to demonstrate this below:

    amazondata = [{'amzlink': 'https://www.amazon.com/dp/B084ZZ7VY3', 'asin': 'B084ZZ7VY3', 'url': 'https://www.amazon.com/s?k=712145360504&s=review-rank'}]
    
    amazonPage = [{'price': '$14.95', 'image': 'https://m.media-amazon.com/images/I/81D1P4QqLfL._AC_SX425_.jpg', 'rating': '4.7 out of 5'}]
    
    amazondata_fields = set(amazondata[0].keys())
    amazonPage_fields = set(amazonPage[0].keys())
    all_fields = amazondata_fields.union(amazonPage_fields)
    print(all_fields)
> {'price', 'rating', 'asin', 'image', 'amzlink', 'url'}

If you are looking to fuse two dictionaries: Use the update method.


    amazondata[0].update(amazonPage[0])
    print(amazondata[0])


> {'amzlink': 'https://www.amazon.com/dp/B084ZZ7VY3',  'asin':
> 'B084ZZ7VY3',  'url':
> 'https://www.amazon.com/s?k=712145360504&s=review-rank',  'price':
> '$14.95',  'image':
> 'https://m.media-amazon.com/images/I/81D1P4QqLfL._AC_SX425_.jpg', 
> 'rating': '4.7 out of 5'}

  • Related