Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dictionary using a single list?

I have a list of url's and headers from a newspaper site in my country. As a general example:

x = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1']

Each URL element has a corresponding sequence of 'news' elements, which can differ in length. In the example above, URL1 has 3 corresponding news and URL3 has only one.

Sometimes a URL has no corresponding "news" element:

y = ['URL4','news1','news2','URL5','URL6','news1']

I can easily find every URL index and the "news" elements of each URL.

My question is: Is it possible to transform this list into a dictionary in which the URL element is the key and the "news" elements are a list/tuple-value?

Expected Output

z = {'URL1':('news1', 'news2', 'news3'),
     'URL2':('news1', 'news2'),
     'URL3':('news1'),
     'URL4':('news1', 'news2'),
     'URL5':(),
     'URL6':('news1')}

I've seen a similar question in this post, but it doesn't solve my problem.

like image 262
Pedro Henrique Avatar asked Aug 15 '19 16:08

Pedro Henrique


People also ask

Can you use a list as a key for a dictionary?

Second, a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.

Can you put a list in a dictionary Python?

Note that the restriction with keys in Python dictionary is only immutable data types can be used as keys, which means we cannot use a dictionary of list as a key .

Can you have a list in a dictionary?

Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.


2 Answers

You can do it like this:

>>> y = ['URL4','news1','news2','URL5','URL6','news1']
>>> result = {}
>>> current_url = None
>>> for entry in y:
...     if entry.startswith('URL'):
...         current_url = entry
...         result[current_url] = ()
...     else:
...         result[current_url] += (entry, )
...         
>>> result
{'URL4': ('news1', 'news2'), 'URL5': (), 'URL6': ('news1',)}
like image 97
ForceBru Avatar answered Oct 04 '22 01:10

ForceBru


You can use itertools.groupby with a key function to identify a URL:

from itertools import groupby
def _key(url):
    return url.startswith("URL") #in the body of _key, write code to identify a URL

data = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1', 'URL4','news1','news2','URL5','URL6','news1']
new_d = [list(b) for _, b in groupby(data, key=_key)]
grouped = [[new_d[i], tuple(new_d[i+1])] for i in range(0, len(new_d), 2)]
result = dict([i for [*c, a], b in grouped for i in [(i, ()) for i in c]+[(a, b)]])

Output:

{
 'URL1': ('news1', 'news2', 'news3'), 
 'URL2': ('news1', 'news2'), 
 'URL3': ('news1',), 
 'URL4': ('news1', 'news2'), 
 'URL5': (), 
 'URL6': ('news1',)
}
like image 31
Ajax1234 Avatar answered Oct 03 '22 23:10

Ajax1234