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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:28:21+00:00 2026-05-20T11:28:21+00:00

I have multiple lists like below [u’a’, 11, u’P’] [u’a’, 11, u’A’] [u’b’, 2,

  • 0

I have multiple lists like below

[u'a', 11, u'P']
[u'a', 11, u'A']
[u'b', 2, u'P']
[u'c', 1, u'P']
[u'c', 2, u'P']
[u'd', 1, u'P']
[u'e', 3, u'P']
[u'f', 2, u'P']
[u'a', 1, u'P']
[u'a', 2, u'P']
[u'b', 1, u'P']
[u'b', 11, u'P']

how to merge above lists,to loop the list to add up like below

[u'a', 11, u'P'] + [u'a', 2, u'P'] + [u'a', 11, u'A'] = ['a',('P' : 13) ,('A': 11)]

[u'b', 2, u'P'] + [u'b', 1, u'P'] + [u'b', 11, u'P'] = ['b',14,p]

output should like below :

['a',('P' : 13) ,('A': 11)]
['b',14,'p']
  • 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-05-20T11:28:21+00:00Added an answer on May 20, 2026 at 11:28 am

    The output that you want looks slightly strange due to the inconsistency between the two cases. You could trivially change this example to get whatever output you want, however:

    lists = [
     [u'a', 11, u'P'],
     [u'a', 11, u'A'],
     [u'b', 2, u'P'],
     [u'c', 1, u'P'],
     [u'c', 2, u'P'],
     [u'd', 1, u'P'],
     [u'e', 3, u'P'],
     [u'f', 2, u'P'],
     [u'a', 1, u'P'],
     [u'a', 2, u'P'],
     [u'b', 1, u'P'],
     [u'b', 11, u'P']]
    
    # Each key in this dictionary will be one of the first elements
    # from the lists shown above.  The values will be dictionaries
    # mapping a letter (one of the third elements in each list) to
    # their total count (i.e. the sum of the second elements matching
    # the other two columns)
    from collections import defaultdict
    results = defaultdict(dict)
    
    for main_key, count, subkey in lists:
        d = results[main_key]
        d[subkey] = d.get(subkey,0) + count
    
    for main_key, values in results.items():
        print main_key, "=>", values
    

    The output is:

    a => {u'A': 11, u'P': 14}
    c => {u'P': 3}
    b => {u'P': 14}
    e => {u'P': 3}
    d => {u'P': 1}
    f => {u'P': 2}
    

    Update: Thanks to sharjeel for suggesting in the comment below that I remove the setdefault by using defaultdict instead.


    Update 2: In your further question in the comments below, you indicate that instead you want as output “[a] set of lists like [[u'a', 11, u'P'], [u'a', 11, u'A']“. (I’m assuming for the moment that you mean a list of lists rather than a set, but that’s almost as easy.) In order to construct such a list of lists , you could replace the loop that prints the values with the following:

    lists_output = []
    
    for main_key, values in results.items():
        for subkey, count in values.items():
           lists_output.append([main_key,count,subkey])
    
    print lists_output
    

    … which will give the output:

    [[u'a', 11, u'A'], [u'a', 14, u'P'], [u'c', 3, u'P'], [u'b', 14, u'P'], [u'e', 3, u'P'],
     [u'd', 1, u'P'], [u'f', 2, u'P']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How should I map multiple columns with List in IBATIS ? I have a
I am in a very peculiar state. I have a list something like below
I have Custom List to display image with it's name like below... public class
I have multiple SharePoint lists and want to display data from them on to
I'm playing with tuples as compile time lists. In How can I have multiple
I have a registration form with common registration fields and two multiple selection Lists
Currently we have dueling select lists where you can select multiple employees from Available
I have multiple dropdown list: @Html.DropDownListFor(x => x.HaveColoSpace.SelectedOptions, new SelectList(Model.HaveColoSpace.Options, Value, Text), new {
I have a list where elements are hidden for multiple reasons, each corresponding to
I have a table with multiple rows in each row is a select list

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.