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

  • Home
  • SEARCH
  • 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 6780199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:28:39+00:00 2026-05-26T16:28:39+00:00

I have two lists: A = [1, 2, 3, 4, 5] B = [6,

  • 0

I have two lists:

A = [1, 2, 3, 4, 5]
B = [6, 7, 8, 9, 10]

And I need to be able to find the sum of the nth terms from both lists i.e. 1+6, 2+7, 3+8 etc

Could someone please tell me how to refer to items in both lists at the same time?

I read somewhere that I could do Sum = a[i] + b[i] but I’m not convinced on how that would work.

  • 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-26T16:28:40+00:00Added an answer on May 26, 2026 at 4:28 pm

    If you know the lists will be the same length, you could do this:

    AB = [A[i] + B[i] for i in range(len(A))]
    

    In Python 2, you might want to use xrange instead of range if your lists are quite large. I think that’s an explicit, simple, readable, obvious way to do it, but some might differ.

    If the lists might be different lengths, you have to decide how you want to handle the extra elements. Let’s say you want to ignore the extra elements of whichever list is longer. Here are three ways to do it:

    AB = [A[i] + B[i] for i in range(min(len(A), len(B)))]
    
    AB = map(sum, zip(A, B))
    
    AB = [a + b for a, b in zip(A, B)]
    

    The downside of using zip is that it will allocate a list of tuples, which can be a lot of memory if your lists are already large. Using for i in xrange with subscripting won’t allocate all that memory, or you can use itertools.izip:

    import itertools
    AB = map(sum, itertools.izip(A, B))
    

    If you instead want to pretend the shorter list is padded with zeros, using itertools.izip_longest is the shortest answer:

    import itertools
    AB = map(sum, itertools.izip_longest(A, B, fillvalue=0))
    

    or

    import itertools
    AB = [a + b for a, b in itertools.izip_longest(A, B, fillvalue=0)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the need to be able to accurately find the months between two
I have two unsorted lists and I need to produce another list which is
I have a list of two-item lists and need to search for things in
Hello I need to check if two lists have same elements in same order
I have two simple beans--FatKid and Hamburgers. I need to be able to not
I have a list wrapper that maintains two Tstringlists and a TClassList I need
i have the following query to list the employees of two table. i need
I have two lists of custom objects and want to update a field for
I have two lists of objects. Each list is already sorted by a property
I have two lists, each on different tabs (sub-sites) of the site. I would

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.