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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:01:27+00:00 2026-06-15T19:01:27+00:00

Let data = [[3,7,2],[1,4,5],[9,8,7]] Let’s say I want to sum the elements for the

  • 0

Let data = [[3,7,2],[1,4,5],[9,8,7]]

Let’s say I want to sum the elements for the indices of each list in the list, like adding numbers in a matrix column to get a single list. I am assuming that all lists in data are equal in length.

    print foo(data)

   [[3,7,2],
    [1,4,5],
    [9,8,7]]
    _______
 >>>[13,19,14]

How can I iterate over the list of lists without getting an index out of range error? Maybe lambda? Thanks!

  • 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-15T19:01:29+00:00Added an answer on June 15, 2026 at 7:01 pm

    You could try this:

    In [9]: l = [[3,7,2],[1,4,5],[9,8,7]]
    
    In [10]: [sum(i) for i in zip(*l)]
    Out[10]: [13, 19, 14]
    

    This uses a combination of zip and * to unpack the list and then zip the items according to their index. You then use a list comprehension to iterate through the groups of similar indices, summing them and returning in their ‘original’ position.

    To hopefully make it a bit more clear, here is what happens when you iterate through zip(*l):

    In [13]: for i in zip(*l):
       ....:     print i
       ....:     
       ....:     
    (3, 1, 9)
    (7, 4, 8)
    (2, 5, 7)
    

    In the case of lists that are of unequal length, you can use itertools.izip_longest with a fillvalue of 0 – this basically fills missing indices with 0, allowing you to sum all ‘columns’:

    In [1]: import itertools
    
    In [2]: l = [[3,7,2],[1,4],[9,8,7,10]]
    
    In [3]: [sum(i) for i in itertools.izip_longest(*l, fillvalue=0)]
    Out[3]: [13, 19, 9, 10]
    

    In this case, here is what iterating over izip_longest would look like:

    In [4]: for i in itertools.izip_longest(*l, fillvalue=0):
       ...:     print i
       ...:     
    (3, 1, 9)
    (7, 4, 8)
    (2, 0, 7)
    (0, 0, 10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say i have a database design like this: for the data: id |
I want to create an ajax call and get some data (let's say a
Let's say data structure looks something like this in debugger (returned from XML-RPC call):
I have a large nested vector that look like this: import Data.Vector let x
Let say I have the following data in R. training = factor(c(1,1,3,2,1,3,2,34,67,34)) test =
Let say if I have this data my_data = [ 10 20 30 40;
Let's say i have this json data. How to transform the tags to a
Let's say I have some simple data y = [[datetime.datetime( 2012,1,1,1,1), 2.1], [datetime.datetime( 2012,1,1,1,2),
All I want is a list of records each with three string fields in
I want to let only Mobile App (iOS) to let data from my server

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.