Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:50:15+00:00 2026-06-06T13:50:15+00:00

Given userplays = { Alice : { AC/DC : 2, The Raconteurs : 3,

  • 0

Given

userplays = { "Alice"   : { "AC/DC" : 2,
                            "The Raconteurs" : 3,
                            "Mogwai" : 1
                          },
              "Bob"     : { "The XX" : 4,
                            "Lady Gaga" : 3,
                            "Mogwai" : 1,
                            "The Raconteurs" : 1
                          },
              "Charlie" : { "AC/DC" : 7,
                            "Lady Gaga" : 7
                          }
            }

get a list of all bands:

['Lady Gaga', 'Mogwai', 'AC/DC', 'The Raconteurs', 'The XX']

I can do

list(set(flatten([ [ band 
                     for band 
                     in playcounts.keys() ] 
                   for playcounts 
                   in userplays.values() ] ) ) )

where flatten is from Flatten (an irregular) list of lists, but is it possible without flatten, using only list/dict comprehensions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:50:18+00:00Added an answer on June 6, 2026 at 1:50 pm

    Another way is to use a dict comprehension (Python 2.7+):

    {k:v for v in userplays.values() for k in v.keys()}.keys()
    

    Produces:

    ['Lady Gaga', 'Mogwai', 'AC/DC', 'The Raconteurs', 'The XX']
    

    At least in Python 3.3, this is also faster:

    import timeit
    
    userplays = { "Alice"   : { "AC/DC" : 2,
                                "The Raconteurs" : 3,
                                "Mogwai" : 1
                              },
                  "Bob"     : { "The XX" : 4,
                                "Lady Gaga" : 3,
                                "Mogwai" : 1,
                                "The Raconteurs" : 1
                              },
                  "Charlie" : { "AC/DC" : 7,
                                "Lady Gaga" : 7
                              }
                }
    
    
    def f1():
        set(b for v in userplays.values() for b in v.keys())
    
    def f2():
        {k:v for v in userplays.values() for k in v.keys()}.keys()    
    
    t1=timeit.Timer(f1).timeit(10000)
    t2=timeit.Timer(f2).timeit(10000)
    faster=abs(t1-t2) / max(t1,t2)
    print("""
    set:                {:.4} seconds
    dict:               {:.4} seconds
    faster of those is  {:.4%} faster
    
    """.format(t1,t2,faster))
    

    Output:

    set:                0.02448 seconds
    dict:               0.01988 seconds
    faster of those is  18.7907% faster
    

    Edit

    Just for pure curiosity, I compared the various way that this can be done in a one-liner.

    Here are the results:

    f1: set from a generator expression
    f2: keys from a dict comprehension
    f3: set comprehension
    f4: set from a list comprehension
    
           rate/s      f4       f1       f2       f3 
    f4    358,650    0.0%   -13.4%   -31.7%   -41.3% 
    f1    414,246   15.5%     0.0%   -21.1%   -32.2% 
    f2    525,230   46.4%    26.8%     0.0%   -14.1% 
    f3    611,158   70.4%    47.5%    16.4%     0.0% 
    

    You can see that the set comprehension is fastest, followed by a dict comprehension.

    Here is the code that generated that Perl style benchmark:

    import timeit
    import locale
    locale.setlocale(locale.LC_NUMERIC, "")
    
    userplays = { "Alice"   : { "AC/DC" : 2,
                                "The Raconteurs" : 3,
                                "Mogwai" : 1
                              },
                  "Bob"     : { "The XX" : 4,
                                "Lady Gaga" : 3,
                                "Mogwai" : 1,
                                "The Raconteurs" : 1
                              },
                  "Charlie" : { "AC/DC" : 7,
                                "Lady Gaga" : 7
                              }
                }
    
    def f1():
        """set from a generator expression"""
        set(b for v in userplays.values() for b in v.keys())
    
    def f2():
        """keys from a dict comprehension"""
        {k:v for v in userplays.values() for k in v.keys()}.keys()    
    
    def f3():
        """set comprehension"""
        {b for v in userplays.values() for b in v.keys()}
    
    def f4():
        """set from a list comprehension"""
        set([b for v in userplays.values() for b in v.keys()])
    
    def test_table(funcs, c):
        results={k.__name__:timeit.Timer(k).timeit(c) for k in funcs}
        fastest=sorted(results,key=results.get, reverse=True)
        table=[]
        table.append([' ','rate/s']+fastest)
        for e in fastest:
            temp=[]
            temp.append(e)
            temp.append(int(round(float(c)/results[e])))
            t2=['{:.1%}'.format((results[x]-results[e])/results[e]) for x in fastest]
            table.append(temp+t2)
        print()    
        for e in funcs:
            print('{}: {}'.format(e.__name__, e.__doc__))
        print()            
        pprint_table(table)    
    
    def format_num(num):
        """Format a number according to given places.
        Adds commas, etc. Will truncate floats into ints!"""
    
        try:
            inum = int(num)
            return locale.format("%.*f", (0, inum), True)
    
        except (ValueError, TypeError):
            return str(num)
    
    def get_max_width(table, index):
        """Get the maximum width of the given column index"""
        return max([len(format_num(row[index])) for row in table])        
    
    def pprint_table(table):
        col_paddings = []
        for i in range(len(table[0])):
            col_paddings.append(get_max_width(table, i))
    
        for row in table:
            # left col
            print(row[0].ljust(col_paddings[0] + 1),end=' ')
            # rest of the cols
            for i in range(1, len(row)):
                col = format_num(row[i]).rjust(col_paddings[i] + 2)
                print (col,end=' ')
            print()
    
    test_table([f1,f2,f3,f4],100000) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a package, how can I automatically find all its sub-packages?
Given an NSApp object (aka [NSApplication sharedApplication]), how can I get the currently active
Given the following : // get the list of the players , in order
Given: class StringRecord : INotifyPropertyChanged { public string Key { get; set; } //
Given the following classes: class Department { public String Name { get; set; }
Given: http://www.foo.com/bar.html#baz How does one get the baz ? I can't find this as
Given the following string: s = 'abcdefg*' How can I match it or any
Given a storyboard-based application, how can any one view controller invoke the methods of
Given a reference DateTime and given a DateTime to be verified, how can I
Given the above dynamically generated text (meaning that I can't just use an image),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.