Put multiple values with same key in one row

89 views Asked by At

I have such result:

{'name': name1 , 'pic': pic1}
{'name': name1 , 'pic': pic2}
{'name': name1 , 'pic': pic3}
{'name': name2 , 'pic': pic4}
{'name': name2 , 'pic': pic5}
{'name': name2 , 'pic': pic6}
{'name': name3 , 'pic': pic7}
{'name': name3 , 'pic': pic8}
{'name': name3 , 'pic': pic9} 
{'name': name3 , 'pic': pic10}

And I need this:

{'name': name1 , 'pic': pic1, pic2, pic3} 
{'name': name2 , 'pic': pic4, pic5, pic6} 
{'name': name3 , 'pic': pic7, pic8, pic9, pic10}

How can I do that?

My code is:

for links in url_list:
    try:
        r = requests.get(links, headers=headers, params=params, timeout=20)
    except requests.exceptions.ConnectionError as err:
        print('Bad status code', r.status_code)
    soup = BeautifulSoup(r.content, 'lxml')

    name = soup.find('h1', class_='name')
    pics = soup.findAll('a', class_='gallery')
    for pic in pics:
        pic = baseurl + pic['href']
        list = {'name': name,'pic': pic}
        print(list)
1

There are 1 answers

0
ScottC On

Here is one way to do it:

Assuming you have your original dictionaries in a list pic_list:

import pandas as pd

name1,name2,name3 = 'name1','name2','name3'
pic1,pic2,pic3,pic4,pic5,pic6,pic7,pic8,pic9,pic10 = 'pic1','pic2','pic3','pic4','pic5','pic6','pic7','pic8','pic9','pic10'

pic_list = [{'name': name1 , 'pic': pic1},
            {'name': name1 , 'pic': pic2},
            {'name': name1 , 'pic': pic3},
            {'name': name2 , 'pic': pic4},
            {'name': name2 , 'pic': pic5},
            {'name': name2 , 'pic': pic6},
            {'name': name3 , 'pic': pic7},
            {'name': name3 , 'pic': pic8},
            {'name': name3 , 'pic': pic9}, 
            {'name': name3 , 'pic': pic10}]

You could :

  • convert the list to a dataframe,
  • group by name,
  • and convert to list
  • then convert back to dictionary (if needed)

Here is the code:

df = pd.DataFrame(pic_list)
new_dict = dict(df.groupby('name')['pic'].apply(list))

for k,v in new_dict.items():
    result = {'name': k, 'pic':v}   
    print(result)

OUTPUT:

{'name': 'name1', 'pic': ['pic1', 'pic2', 'pic3']}
{'name': 'name2', 'pic': ['pic4', 'pic5', 'pic6']}
{'name': 'name3', 'pic': ['pic7', 'pic8', 'pic9', 'pic10']}