Search code examples
pythonpython-2.7nested-lists

Counting number of elements in nested list


Im trying to count the number of elements in a nested list, the list looks like this:

[(1944, ['Hughes H']),
 (1940, ['Hill DK', 'Crawford GN', 'Greene HS', 'Myers J', 'Burr GO']),
 (1941,
  ['McClung CE',
   'Sumner FB',
   'Gates RR',
   'Lewis WH',
   'Haas O',
   'Haas O',
   'Gould BS',
   'Tytell AA',
   'Hatch MH']),
 (1942,
  ['Gaffron H',
   'Gardner FT',
   'Edwards PR',
   'Bruner DW',
   'Lake NC',
   'Ratner B',
   'Gaffron H',
   'Rubin J',
   'Ritter WE']),
 (1943,
  ['Bousfield G',
   'Fishbein M',
   'Faber HK',
   'Silverberg RJ',
   'Dong L',
   'Howorth MB'])]

This is the code is used to get this output:

d = defaultdict(list)
for k, v in authors_expanded:
        d[k].append(v)

d.items()

Using the following code works and just subtracting one works:

len(d.items())-1

Since the first element of the list always contains one item. I'm looking for a nicer solution tough.

Providing me a good link would be great too, i just can't seem to find any myself.


Solution

  • If you're looking for the number of authors per year, you could use this:

    # Authors per year
    authors_per_year = { year: len(authors) for year, authors in the_list }
    

    Gives you this:

    {1940: 5, 1941: 9, 1942: 9, 1943: 6, 1944: 1}
    

    Or, if you looking for a count of unique authors then you could use this:

    # Unique authors
    unique_authors = set([ a for year, authors in the_list 
                               for a in authors])
    

    Gives you this set:

    set(['Bousfield G',
         'Bruner DW',
         'Burr GO',
         'Crawford GN',
         'Dong L',
         'Edwards PR',
         'Faber HK',
         'Fishbein M',
         'Gaffron H',
         'Gardner FT',
         'Gates RR',
         'Gould BS',
         'Greene HS',
         'Haas O',
         'Hatch MH',
         'Hill DK',
         'Howorth MB',
         'Hughes H',
         'Lake NC',
         'Lewis WH',
         'McClung CE',
         'Myers J',
         'Ratner B',
         'Ritter WE',
         'Rubin J',
         'Silverberg RJ',
         'Sumner FB',
         'Tytell AA'])
    

    So len(unique_authors) gives you a count of 28.

    In any case, I think the way forward for you may well be to use some combination of list comprehensions or dict comprehension.