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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:50:17+00:00 2026-05-20T05:50:17+00:00

Suppose I have two arrays: arrayOne = [[james, 35], [michael, 28], [steven, 23], [jack,

  • 0

Suppose I have two arrays:

arrayOne = [["james", 35], ["michael", 28], ["steven", 23], 
            ["jack", 18], ["robert", 12]]
arrayTwo = [["charles", 45], ["james",  36], ["trevor", 24], 
            ["michael", 17], ["steven", 4]]

I want to merge them, so that I would have a single 2D array, where the first element of each inner array is the name (james, charles, etc). The second element of the inner array is its respective value in arrayOne, and if does not have a respective value it would be 0. Conversely for the third element. The order does not really matter as long as the numbers match the name. In other words I would get something like this

arrayResult = [["james", 35, 36], ["michael", 28, 17], ["steven", 23, 4],
               ["jack", 18, 0], ["robert", 12, 0], ["charles", 0, 45],
               ["trevor", 0, 4]]

Also, I am trying to have it so that I could add more “columns” to this array result if I were to give another array.

  • 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-20T05:50:17+00:00Added an answer on May 20, 2026 at 5:50 am

    It looks like what you really need is dictionaries, rather than arrays. If you use a dictionary, this problem becomes a whole lot easier. Converting to dicts couldn’t be easier:

    dictOne = dict(arrayOne)
    dictTwo = dict(arrayTwo)
    

    From there, you can put them together like this:

    combined = dict()
    for name in set(dictOne.keys() + dictTwo.keys()):
      combined[name] = [ dictOne.get(name, 0), dictTwo.get(name, 0) ]
    

    What this does is create a new dictionary called combined, which we’ll put the final data in. Then, we make a set of keys from both original dictionaries. Using a set ensures we don’t do anything twice. Finally, we loop through this set of keys and add each pair of values to the combined dictionary, telling calls to the .get method to supply 0 if no value is present. If you need to switch the combined dictionary back to an array, that’s pretty easy too:

    arrayResult = []
    for name in combined:
      arrayResult.append([ name ] + combined[name])
    

    Supposing you want to add another column to your result dictionary, all you have to do is change the middle code to look like this:

    combined = dict()
    for name in set(dictOne.keys() + dictTwo.keys() + dictThree.keys()):
      combined[name] = [ dictOne.get(name, 0), dictTwo.get(name, 0), dictThree.get(name, 0) ]
    

    If you wanted to encapsulate all this logic in a function (which is something I would recommend), you could do it like this:

    def combine(*args):
      # Create a list of dictionaries from the arrays we passed in, since we are
      # going to use dictionaries to solve the problem.
      dicts = [ dict(a) for a in args ]
    
      # Create a list of names by looping through all dictionaries, and through all
      # the names in each dictionary, adding to a master list of names
      names = []
      for d in dicts:
        for name in d.keys():
          names.append(name)
    
      # Remove duplicates in our list of names by making it a set
      names = set(names)
    
      # Create a result dict to store results in
      result = dict()
    
      # Loop through all the names, and add a row for each name, pulling data from
      # each dict we created in the beginning
      for name in names:
        result[name] = [ d.get(name, 0) for d in dicts ]
    
      # Return, secure in the knowledge of a job well done. :-)
      return result
    
    # Use the function:
    resultDict = combine(arrayOne, arrayTwo, arrayThree)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that you have two huge files (several GB) that you want to concatenate
Suppose that I have two numpy arrays of the form x = [[1,2] [2,4]
Suppose I have two applications written in C#. The first is a third party
Suppose you have two models, User and City, joined by a third model CityPermission:
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose you have two seperate ASP.NET Web Application projects that both need to use
Suppose I have two branches of a project IMClient-MacOS and IMClient-Windows, and their code
Suppose I have the following two strings containing regular expressions. How do I coalesce
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have a tags table with two columns: tagid and contentid . Each

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.