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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:08:11+00:00 2026-06-16T00:08:11+00:00

I am a new python user, and I need help about combining list elements

  • 0

I am a new python user, and I need help about combining list elements under a condition.
I have a list like this:

x = [['a', 10, 20], ['b', 10, 20], ['a', 20, 100]]

I would like to combine list elements which start with the same letter in a list by summing up the other elements. for example, I’d like to obtain this list for x:

x = [['a', 30, 120], ['b', 10, 20]]

How can I achieve this ?

  • 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-16T00:08:12+00:00Added an answer on June 16, 2026 at 12:08 am

    A one-liner using itertools.groupby():

    In [45]: lis=[['a', 10, 20], ['b', 10, 20], ['a', 20, 100]]
    
    In [46]: lis.sort(key=itemgetter(0)) #sort the list first
    
    In [47]: lis
    Out[47]: [['a', 10, 20], ['a', 20, 100], ['b', 10, 20]]
    
    In [49]: [[k]+map(sum,zip(*[x[1:] for x in g])) for k,g in groupby(lis,key=itemgetter(0))]
    Out[49]: [['a', 30, 120], ['b', 10, 20]]
    

    A simple solution:

    In [23]: lis=[['a', 10, 20], ['b', 10, 20], ['a', 20, 100]]
    
    In [24]: ans=[]
    
    In [25]: lis.sort(key=itemgetter(0))   #sort the list according to the first elem
    
    In [26]: lis
    Out[26]: [['a', 10, 20], ['a', 20, 100], ['b', 10, 20]]
    
    In [27]: for x in lis:
        if ans:
            if x[0]==ans[-1][0]:  #if the value of the first elem of last element in ans is same as x[0]
                ans[-1][1]+=x[1]
                ans[-1][2]+=x[2]
            else:         
                ans.append(x)
        else:ans.append(x)
       ....:     
    
    In [28]: ans
    Out[28]: [['a', 30, 120], ['b', 10, 20]]
    

    Without sorting the list using defaultdict():

    In [69]: dic=defaultdict(list)
    
    In [70]: for x in lis:
        dic[x[0]].append(x[1:])
       ....:     
    
    In [71]: dic
    Out[71]: defaultdict(<type 'list'>, {'a': [[10, 20], [20, 100]], 'b': [[10, 20]]})
    
    In [72]: [[k]+map(sum,zip(*i)) for k,i in dic.items()]
    Out[72]: [['a', 30, 120], ['b', 10, 20]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python programming. I have this problem: I have a list of
I'm new in python and need help. I have a file and want to
Very new to python and can't understand why this isn't working. I have a
I know this is simple, but I'm a new user to Python so I'm
Hello I am a new user to Python and I am having problem doing
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
Brand new to Python (and programming in general), if this is simple and/or answered
I am new to Python and have been studying its fundementals for 3 months
I'm a new to Python. I have installed Python 2.7 64 bit on Win7
I have a Python script that we're using to parse CSV files with user-entered

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.