Group array by the first element value Python

3.8k Views Asked by At

i have an Array arr = [title, fileurl] so when i do print arr it goes like this:

['name1', 'url1']
['name1', 'url2']
['name1', 'url3']
['name2', 'url1']
['name2', 'url2']
['name3', 'url1']

I would like to group these array by the first element, it means I would like to have:

['name1', 'url1', 'url2', 'url3']
['name2', 'url1', 'url2']
['name3', 'url1']

My code:

for final in posterlink:
    pagesourcec = requests.get(final)
    soupc = BeautifulSoup(pagesourcec.text, "html.parser")
    strc = soupc.findAll("iframe", attrs={"id": "myframe"})
    title = soupb.find("li",{"class": "breadcrumb-item active"}).get_text()
    for embedlink in strc:
        fff = embedlink.get('data-src')
        arr = [title, fff]
        print arr 
2

There are 2 best solutions below

0
Silver On

You can do this:

from collections import defaultdict as ddict

group = ddict(list)

for name, url in arr:
  group[name].append(url)

And if you absolutely want it as a list of lists, you can then follow up with this:

group = [[name, *urls] for name, urls in group.items()]

Edit: It's important to note that the above line works with python 3, which is what you should be using anyways. However, for the sake of completeness if you're using python 2.7, then use this:

group = [[name] + urls for name, urls in group.items()]
6
Rohit-Pandey On

Try This:

a = [['name1', 'url1'],
 ['name1', 'url2'],
 ['name1', 'url3'],
 ['name2', 'url1'],
 ['name2', 'url2'],
 ['name3', 'url1']]
d = {}
for elem in a:
    if elem[0] not in d:
        d[elem[0]] = []
    d[elem[0]].append(elem[1:])

Output:

{'name1': [['url1'], ['url2'], ['url3']], 'name2': [['url1'], ['url2']], 'name3': [['url1']]}