How to create a dictionary using a single list?Python - Creating a dictionary using ListHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I safely create a nested directory?How do I sort a dictionary by value?How to make a flat list out of list of listsCheck if a given key already exists in a dictionaryCreate a dictionary with list comprehensionHow do I list all files of a directory?Iterating over dictionaries using 'for' loops

Number in overlapping range

Why do my bicycle brakes get worse and feel more 'squishy" over time?

The more + the + comparative degree

Are there any cons in using rounded corners for bar graphs?

Bringing Power Supplies on Plane?

Souce that you can't you tell your wife not to lend to others?

How can I communicate my issues with a potential date's pushy behavior?

Why do so many people play out of turn on the last lead?

How can I find files in directories listed in a file?

Setting up a Mathematical Institute of Refereeing?

Heyawake: An Introductory Puzzle

A man in the desert is bitten by a skeletal animal, its skull gets stuck on his arm

When did Bilbo and Frodo learn that Gandalf was a Maia?

What is the opposite of "hunger level"?

Why are electric shavers specifically permitted under FAR §91.21

Is there a name for the technique in songs/poems, where the rhyming pattern primes the listener for a certain line, which never comes?

Would the USA be eligible to join the European Union?

Why does this Jet Provost strikemaster have a textured leading edge?

What is a "soap"?

Is there a fallacy about "appeal to 'big words'"?

What if a restaurant suddenly cannot accept credit cards, and the customer has no cash?

Output the list of musical notes

What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?

Why aren’t there water shutoff valves for each room?



How to create a dictionary using a single list?


Python - Creating a dictionary using ListHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I safely create a nested directory?How do I sort a dictionary by value?How to make a flat list out of list of listsCheck if a given key already exists in a dictionaryCreate a dictionary with list comprehensionHow do I list all files of a directory?Iterating over dictionaries using 'for' loops






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6















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 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.










share|improve this question



















  • 3





    Please include the code you wrote that does not produce the desired output.

    – dfundako
    8 hours ago











  • It's possible, but there probably isn't anything particular elegant like dict(foo(bar(baz(x)))) for some set of functions foo, bar, and baz.

    – chepner
    8 hours ago












  • Are you generating x? If so, there must be a better way to do it.

    – DeepSpace
    8 hours ago











  • @DeepSpace I'm scrapping a web-site using Selenium and i though that using list in this way were easier to work with. But it isn't.

    – Pedro Henrique
    8 hours ago

















6















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 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.










share|improve this question



















  • 3





    Please include the code you wrote that does not produce the desired output.

    – dfundako
    8 hours ago











  • It's possible, but there probably isn't anything particular elegant like dict(foo(bar(baz(x)))) for some set of functions foo, bar, and baz.

    – chepner
    8 hours ago












  • Are you generating x? If so, there must be a better way to do it.

    – DeepSpace
    8 hours ago











  • @DeepSpace I'm scrapping a web-site using Selenium and i though that using list in this way were easier to work with. But it isn't.

    – Pedro Henrique
    8 hours ago













6












6








6








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 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.










share|improve this question














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 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.







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









Pedro HenriquePedro Henrique

1488 bronze badges




1488 bronze badges










  • 3





    Please include the code you wrote that does not produce the desired output.

    – dfundako
    8 hours ago











  • It's possible, but there probably isn't anything particular elegant like dict(foo(bar(baz(x)))) for some set of functions foo, bar, and baz.

    – chepner
    8 hours ago












  • Are you generating x? If so, there must be a better way to do it.

    – DeepSpace
    8 hours ago











  • @DeepSpace I'm scrapping a web-site using Selenium and i though that using list in this way were easier to work with. But it isn't.

    – Pedro Henrique
    8 hours ago












  • 3





    Please include the code you wrote that does not produce the desired output.

    – dfundako
    8 hours ago











  • It's possible, but there probably isn't anything particular elegant like dict(foo(bar(baz(x)))) for some set of functions foo, bar, and baz.

    – chepner
    8 hours ago












  • Are you generating x? If so, there must be a better way to do it.

    – DeepSpace
    8 hours ago











  • @DeepSpace I'm scrapping a web-site using Selenium and i though that using list in this way were easier to work with. But it isn't.

    – Pedro Henrique
    8 hours ago







3




3





Please include the code you wrote that does not produce the desired output.

– dfundako
8 hours ago





Please include the code you wrote that does not produce the desired output.

– dfundako
8 hours ago













It's possible, but there probably isn't anything particular elegant like dict(foo(bar(baz(x)))) for some set of functions foo, bar, and baz.

– chepner
8 hours ago






It's possible, but there probably isn't anything particular elegant like dict(foo(bar(baz(x)))) for some set of functions foo, bar, and baz.

– chepner
8 hours ago














Are you generating x? If so, there must be a better way to do it.

– DeepSpace
8 hours ago





Are you generating x? If so, there must be a better way to do it.

– DeepSpace
8 hours ago













@DeepSpace I'm scrapping a web-site using Selenium and i though that using list in this way were easier to work with. But it isn't.

– Pedro Henrique
8 hours ago





@DeepSpace I'm scrapping a web-site using Selenium and i though that using list in this way were easier to work with. But it isn't.

– Pedro Henrique
8 hours ago












5 Answers
5






active

oldest

votes


















9














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',)





share|improve this answer




















  • 1





    IMHO, that's a very clean and easy to read approach.

    – jjramsey
    8 hours ago






  • 1





    You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

    – DeepSpace
    8 hours ago












  • @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

    – ForceBru
    8 hours ago











  • @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

    – Pedro Henrique
    8 hours ago


















2














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',)






share|improve this answer
































    2














    You can just use the indices of the URL keys in the list and grab what is between the indices and assign to the first



    Like this:



    x = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1']
    urls = [x.index(y) for y in x if 'URL' in y]
    adict =
    for i in range(0, len(urls)):
    if i == len(urls)-1:
    adict[x[urls[i]]] = x[urls[i]+1:len(x)]
    else:
    adict[x[urls[i]]] = x[urls[i]+1:urls[i+1]]
    print(adict)


    output:



    'URL1': ['news1', 'news2', 'news3'], 'URL2': ['news1', 'news2'], 'URL3': ['news1']





    share|improve this answer

























    • if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

      – jjramsey
      8 hours ago











    • @jjramsey but that's not in his list.

      – Anna Nevison
      8 hours ago











    • True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

      – jjramsey
      8 hours ago












    • @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

      – Anna Nevison
      8 hours ago






    • 1





      @jjramsey I should have pointed out that the news items don't have 'URL' in them.

      – Pedro Henrique
      8 hours ago


















    0














    Another solution using groupby, one-liner:



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

    from itertools import groupby

    out = k: tuple(v) for _, (k, *v) in groupby(x, lambda k, d='g':0: (d.update(g=d['g']+1), d['g']) if k.startswith('URL') else (None, d['g']))

    from pprint import pprint
    pprint(out)


    Prints:



    'URL1': ('news1', 'news2', 'news3'),
    'URL2': ('news1', 'news2'),
    'URL3': ('news1',),
    'URL4': ('news1', 'news2'),
    'URL5': (),
    'URL6': ('news1',)





    share|improve this answer
































      0














      The more-itertools library contains a function split_before() which comes in very handy for this purpose:



      s[0]: tuple(s[1:]) for s in mt.split_before(x, lambda e: e.startswith('URL'))


      I think this is cleaner than any of the other approaches in answers posted before this one, but it does introduce an external dependency (unless you reimplement the function), which makes it not appropriate for every situation.



      If your actual use case involves real URLs or something else, rather than strings of the form URL#, then just replace lambda e: e.startswith('URL') with whatever function you can use to select the key elements apart from the value elements.





      share



























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57512789%2fhow-to-create-a-dictionary-using-a-single-list%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        9














        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',)





        share|improve this answer




















        • 1





          IMHO, that's a very clean and easy to read approach.

          – jjramsey
          8 hours ago






        • 1





          You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

          – DeepSpace
          8 hours ago












        • @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

          – ForceBru
          8 hours ago











        • @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

          – Pedro Henrique
          8 hours ago















        9














        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',)





        share|improve this answer




















        • 1





          IMHO, that's a very clean and easy to read approach.

          – jjramsey
          8 hours ago






        • 1





          You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

          – DeepSpace
          8 hours ago












        • @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

          – ForceBru
          8 hours ago











        • @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

          – Pedro Henrique
          8 hours ago













        9












        9








        9







        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',)





        share|improve this answer













        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',)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        ForceBruForceBru

        24.3k9 gold badges36 silver badges59 bronze badges




        24.3k9 gold badges36 silver badges59 bronze badges










        • 1





          IMHO, that's a very clean and easy to read approach.

          – jjramsey
          8 hours ago






        • 1





          You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

          – DeepSpace
          8 hours ago












        • @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

          – ForceBru
          8 hours ago











        • @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

          – Pedro Henrique
          8 hours ago












        • 1





          IMHO, that's a very clean and easy to read approach.

          – jjramsey
          8 hours ago






        • 1





          You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

          – DeepSpace
          8 hours ago












        • @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

          – ForceBru
          8 hours ago











        • @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

          – Pedro Henrique
          8 hours ago







        1




        1





        IMHO, that's a very clean and easy to read approach.

        – jjramsey
        8 hours ago





        IMHO, that's a very clean and easy to read approach.

        – jjramsey
        8 hours ago




        1




        1





        You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

        – DeepSpace
        8 hours ago






        You can use deafultdict(list) to save at least 4 lines. I'm not sure why you opted to use tuples if you knew you needed to add new items

        – DeepSpace
        8 hours ago














        @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

        – ForceBru
        8 hours ago





        @DeepSpace, the OP wanted tuples, so here they are! I was using lists originally but then edited the code to use tuples. As for defaultdict - absolutely; I'm always forgetting about it.

        – ForceBru
        8 hours ago













        @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

        – Pedro Henrique
        8 hours ago





        @DeepSpace I see your point about using tuples. I rearranged ForceBru's answer to fit arrays instead.

        – Pedro Henrique
        8 hours ago













        2














        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',)






        share|improve this answer





























          2














          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',)






          share|improve this answer



























            2












            2








            2







            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',)






            share|improve this answer













            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',)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 8 hours ago









            Ajax1234Ajax1234

            46.8k4 gold badges31 silver badges61 bronze badges




            46.8k4 gold badges31 silver badges61 bronze badges
























                2














                You can just use the indices of the URL keys in the list and grab what is between the indices and assign to the first



                Like this:



                x = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1']
                urls = [x.index(y) for y in x if 'URL' in y]
                adict =
                for i in range(0, len(urls)):
                if i == len(urls)-1:
                adict[x[urls[i]]] = x[urls[i]+1:len(x)]
                else:
                adict[x[urls[i]]] = x[urls[i]+1:urls[i+1]]
                print(adict)


                output:



                'URL1': ['news1', 'news2', 'news3'], 'URL2': ['news1', 'news2'], 'URL3': ['news1']





                share|improve this answer

























                • if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

                  – jjramsey
                  8 hours ago











                • @jjramsey but that's not in his list.

                  – Anna Nevison
                  8 hours ago











                • True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

                  – jjramsey
                  8 hours ago












                • @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

                  – Anna Nevison
                  8 hours ago






                • 1





                  @jjramsey I should have pointed out that the news items don't have 'URL' in them.

                  – Pedro Henrique
                  8 hours ago















                2














                You can just use the indices of the URL keys in the list and grab what is between the indices and assign to the first



                Like this:



                x = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1']
                urls = [x.index(y) for y in x if 'URL' in y]
                adict =
                for i in range(0, len(urls)):
                if i == len(urls)-1:
                adict[x[urls[i]]] = x[urls[i]+1:len(x)]
                else:
                adict[x[urls[i]]] = x[urls[i]+1:urls[i+1]]
                print(adict)


                output:



                'URL1': ['news1', 'news2', 'news3'], 'URL2': ['news1', 'news2'], 'URL3': ['news1']





                share|improve this answer

























                • if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

                  – jjramsey
                  8 hours ago











                • @jjramsey but that's not in his list.

                  – Anna Nevison
                  8 hours ago











                • True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

                  – jjramsey
                  8 hours ago












                • @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

                  – Anna Nevison
                  8 hours ago






                • 1





                  @jjramsey I should have pointed out that the news items don't have 'URL' in them.

                  – Pedro Henrique
                  8 hours ago













                2












                2








                2







                You can just use the indices of the URL keys in the list and grab what is between the indices and assign to the first



                Like this:



                x = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1']
                urls = [x.index(y) for y in x if 'URL' in y]
                adict =
                for i in range(0, len(urls)):
                if i == len(urls)-1:
                adict[x[urls[i]]] = x[urls[i]+1:len(x)]
                else:
                adict[x[urls[i]]] = x[urls[i]+1:urls[i+1]]
                print(adict)


                output:



                'URL1': ['news1', 'news2', 'news3'], 'URL2': ['news1', 'news2'], 'URL3': ['news1']





                share|improve this answer













                You can just use the indices of the URL keys in the list and grab what is between the indices and assign to the first



                Like this:



                x = ['URL1','news1','news2','news3','URL2','news1','news2','URL3','news1']
                urls = [x.index(y) for y in x if 'URL' in y]
                adict =
                for i in range(0, len(urls)):
                if i == len(urls)-1:
                adict[x[urls[i]]] = x[urls[i]+1:len(x)]
                else:
                adict[x[urls[i]]] = x[urls[i]+1:urls[i+1]]
                print(adict)


                output:



                'URL1': ['news1', 'news2', 'news3'], 'URL2': ['news1', 'news2'], 'URL3': ['news1']






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 8 hours ago









                Anna NevisonAnna Nevison

                87613 bronze badges




                87613 bronze badges















                • if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

                  – jjramsey
                  8 hours ago











                • @jjramsey but that's not in his list.

                  – Anna Nevison
                  8 hours ago











                • True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

                  – jjramsey
                  8 hours ago












                • @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

                  – Anna Nevison
                  8 hours ago






                • 1





                  @jjramsey I should have pointed out that the news items don't have 'URL' in them.

                  – Pedro Henrique
                  8 hours ago

















                • if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

                  – jjramsey
                  8 hours ago











                • @jjramsey but that's not in his list.

                  – Anna Nevison
                  8 hours ago











                • True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

                  – jjramsey
                  8 hours ago












                • @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

                  – Anna Nevison
                  8 hours ago






                • 1





                  @jjramsey I should have pointed out that the news items don't have 'URL' in them.

                  – Pedro Henrique
                  8 hours ago
















                if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

                – jjramsey
                8 hours ago





                if 'URL' in y would also be True for a string like http://mytinyURL.com, which is not what you want.

                – jjramsey
                8 hours ago













                @jjramsey but that's not in his list.

                – Anna Nevison
                8 hours ago





                @jjramsey but that's not in his list.

                – Anna Nevison
                8 hours ago













                True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

                – jjramsey
                8 hours ago






                True, but the items 'news1', 'news2', etc. are clearly placeholders for items that may contain nearly arbitrary text, including a string containing the characters 'URL'.

                – jjramsey
                8 hours ago














                @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

                – Anna Nevison
                8 hours ago





                @jjramsey he can obviously modify it based on his use case. That's like saying this specific code wouldn't work if 'Cat' was one of the URLs-it's arbitrary. the idea is to use the indices and find something unique about the urls to be able to get them.

                – Anna Nevison
                8 hours ago




                1




                1





                @jjramsey I should have pointed out that the news items don't have 'URL' in them.

                – Pedro Henrique
                8 hours ago





                @jjramsey I should have pointed out that the news items don't have 'URL' in them.

                – Pedro Henrique
                8 hours ago











                0














                Another solution using groupby, one-liner:



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

                from itertools import groupby

                out = k: tuple(v) for _, (k, *v) in groupby(x, lambda k, d='g':0: (d.update(g=d['g']+1), d['g']) if k.startswith('URL') else (None, d['g']))

                from pprint import pprint
                pprint(out)


                Prints:



                'URL1': ('news1', 'news2', 'news3'),
                'URL2': ('news1', 'news2'),
                'URL3': ('news1',),
                'URL4': ('news1', 'news2'),
                'URL5': (),
                'URL6': ('news1',)





                share|improve this answer





























                  0














                  Another solution using groupby, one-liner:



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

                  from itertools import groupby

                  out = k: tuple(v) for _, (k, *v) in groupby(x, lambda k, d='g':0: (d.update(g=d['g']+1), d['g']) if k.startswith('URL') else (None, d['g']))

                  from pprint import pprint
                  pprint(out)


                  Prints:



                  'URL1': ('news1', 'news2', 'news3'),
                  'URL2': ('news1', 'news2'),
                  'URL3': ('news1',),
                  'URL4': ('news1', 'news2'),
                  'URL5': (),
                  'URL6': ('news1',)





                  share|improve this answer



























                    0












                    0








                    0







                    Another solution using groupby, one-liner:



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

                    from itertools import groupby

                    out = k: tuple(v) for _, (k, *v) in groupby(x, lambda k, d='g':0: (d.update(g=d['g']+1), d['g']) if k.startswith('URL') else (None, d['g']))

                    from pprint import pprint
                    pprint(out)


                    Prints:



                    'URL1': ('news1', 'news2', 'news3'),
                    'URL2': ('news1', 'news2'),
                    'URL3': ('news1',),
                    'URL4': ('news1', 'news2'),
                    'URL5': (),
                    'URL6': ('news1',)





                    share|improve this answer













                    Another solution using groupby, one-liner:



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

                    from itertools import groupby

                    out = k: tuple(v) for _, (k, *v) in groupby(x, lambda k, d='g':0: (d.update(g=d['g']+1), d['g']) if k.startswith('URL') else (None, d['g']))

                    from pprint import pprint
                    pprint(out)


                    Prints:



                    'URL1': ('news1', 'news2', 'news3'),
                    'URL2': ('news1', 'news2'),
                    'URL3': ('news1',),
                    'URL4': ('news1', 'news2'),
                    'URL5': (),
                    'URL6': ('news1',)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 8 hours ago









                    Andrej KeselyAndrej Kesely

                    18.8k2 gold badges10 silver badges34 bronze badges




                    18.8k2 gold badges10 silver badges34 bronze badges
























                        0














                        The more-itertools library contains a function split_before() which comes in very handy for this purpose:



                        s[0]: tuple(s[1:]) for s in mt.split_before(x, lambda e: e.startswith('URL'))


                        I think this is cleaner than any of the other approaches in answers posted before this one, but it does introduce an external dependency (unless you reimplement the function), which makes it not appropriate for every situation.



                        If your actual use case involves real URLs or something else, rather than strings of the form URL#, then just replace lambda e: e.startswith('URL') with whatever function you can use to select the key elements apart from the value elements.





                        share





























                          0














                          The more-itertools library contains a function split_before() which comes in very handy for this purpose:



                          s[0]: tuple(s[1:]) for s in mt.split_before(x, lambda e: e.startswith('URL'))


                          I think this is cleaner than any of the other approaches in answers posted before this one, but it does introduce an external dependency (unless you reimplement the function), which makes it not appropriate for every situation.



                          If your actual use case involves real URLs or something else, rather than strings of the form URL#, then just replace lambda e: e.startswith('URL') with whatever function you can use to select the key elements apart from the value elements.





                          share



























                            0












                            0








                            0







                            The more-itertools library contains a function split_before() which comes in very handy for this purpose:



                            s[0]: tuple(s[1:]) for s in mt.split_before(x, lambda e: e.startswith('URL'))


                            I think this is cleaner than any of the other approaches in answers posted before this one, but it does introduce an external dependency (unless you reimplement the function), which makes it not appropriate for every situation.



                            If your actual use case involves real URLs or something else, rather than strings of the form URL#, then just replace lambda e: e.startswith('URL') with whatever function you can use to select the key elements apart from the value elements.





                            share













                            The more-itertools library contains a function split_before() which comes in very handy for this purpose:



                            s[0]: tuple(s[1:]) for s in mt.split_before(x, lambda e: e.startswith('URL'))


                            I think this is cleaner than any of the other approaches in answers posted before this one, but it does introduce an external dependency (unless you reimplement the function), which makes it not appropriate for every situation.



                            If your actual use case involves real URLs or something else, rather than strings of the form URL#, then just replace lambda e: e.startswith('URL') with whatever function you can use to select the key elements apart from the value elements.






                            share











                            share


                            share










                            answered 6 mins ago









                            David ZDavid Z

                            100k20 gold badges205 silver badges245 bronze badges




                            100k20 gold badges205 silver badges245 bronze badges






























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57512789%2fhow-to-create-a-dictionary-using-a-single-list%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單