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 8310401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:26:49+00:00 2026-06-08T19:26:49+00:00

Suppose I have two lists: l1 = [[‘b’, (1, 1)], [‘b’, (1, 2)], [‘b’,

  • 0

Suppose I have two lists:

l1 = [['b', (1, 1)], ['b', (1, 2)], ['b', (1, 3)], ['a', (1, 5)],
      ['b', (2, 1)], ['b',(3, 1)]]

l2 = ['A','B','C']

How can I create a dictionary into this format?

dct = {'A': len(sublist1), 'B': len(sublist2), 'C' : len(sublist3)}

where

sublist1 = [['b', (1, 1)], ['b', (1, 2)], ['b', (1, 3)], ['a', (1, 5)]]
sublist2 = [['b', (2, 1)]]
sublist3 = [['b',(3, 1)]]

what will happen if my l1 is as given below:

ls1 = [[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2)]]    

then my output should be:

dct = {'A': len(sublist1), 'B': len(sublist2)}

where

sublist1 = [[(1, 1),(1, 2),(1, 3),(1, 4)]]
sublist2 = [[(2, 1),(2, 2),(2, 3)]]

Can the overall problem can be solved in generic way?

  • 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-08T19:26:51+00:00Added an answer on June 8, 2026 at 7:26 pm

    This seems to work:

    from itertools import groupby
    
    key = lambda x: x[1][0]
    lens = [len(list(g)) for k, g in groupby(sorted(l1, key=key), key=key)]
    dct = dict(zip(l2, lens))
    

    I hope I’ve inferred correctly when I’ve assumed A match with 1, B with 2, and so on.

    Re: OP edit

    I don’t know where the (2, 3) item in your sublist2 comes from, I assume it’s a mistake. Also, I assume the single element list ls1 was in fact meant to be the direct container of the tuples, since using nested lists here serves no purpose. If so, here’s my proposal for a generic solution:

    from itertools import groupby
    from string import ascii_uppercase
    
    key = lambda x: x[0]
    lens = [len(list(g)) for k, g in groupby(sorted(l1, key=key), key=key)]
    dct = dict(zip(ascii_uppercase, lens))
    

    So, no big changes. The zip function will, when given arguments of unequal length, return a list with the same length as the shortest argument, and this behaviour suits us fine in this situation.

    Keep in mind that if there are more than 26 different values of the first tuple element, then this solution will break, and simply disregard any values larger than the 26th.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists-of-lists which can be schematically summarized by this: a = {{1},
Suppose I have a simple method like this for processing two lists: public static
Suppose I have two lists that holds the list of source file names and
Suppose I have two differently-sized lists a = [1, 2, 3] b = ['a',
Suppose I have a simple to-do list application. The application contains two models: lists
Suppose I have two lists of numbers in files f1, f2, each number one
Suppose you have two arrayLists: A and B. How do I create a new
Suppose I have two C++ functions for debug output: void Trace( const wchar_t* format,
Suppose I have two lists, L and M. Now I want to know if
Suppose I have two lists of type MyCustomType , a class which could be

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.