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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:46:00+00:00 2026-05-30T07:46:00+00:00

In Clojure, I would like to combine several maps into a single map where

  • 0

In Clojure, I would like to combine several maps into a single map where mappings with same key are combined into a list.

For example:

{:humor :happy} {:humor :sad} {:humor :happy} {:weather :sunny}

should lead to:

{:weather :sunny, :humor (:happy :sad :happy)}

I thought about:

(merge-with (comp flatten list) data)

But it is not efficient because flatten has O(n) complexity.

Then I came up with:

(defn agg[x y] (if (coll? x) (cons y x) (list y x)))
(merge-with agg data)

But it feels not idiomatic. Any other idea?

  • 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-30T07:46:01+00:00Added an answer on May 30, 2026 at 7:46 am

    One approach would be

    (defn merge-lists [& maps]
      (reduce (fn [m1 m2]
                (reduce (fn [m [k v]]
                          (update-in m [k] (fnil conj []) v))
                        m1, m2))
              {}
              maps))
    

    It’s a bit ugly, but that’s only because your values aren’t already lists. It also forces everything to be a list (so you’d get :weather [:sunny] rather than :weather :sunny). Frankly, this is likely to be a million times easier for you to work with anyway.

    If you had each value as a vector already, you could simply do (apply merge-with into maps). So, another thing you could try would be to convert to vectors first, and then do the simple merge-with. This probably performs a bit worse than the first approach because of the intermediate data structures.

    (defn merge-lists [& maps]
      (apply merge-with into
             (for [m maps, [k v] m]
               {k [v]})))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I would like to do (in Clojure): For example, I have a vector
I would like to embed Clojure code into Java. This site was helpful in
I would like to know how to list all functions of a Clojure namespace.
I would like to move data back and fourth between clojure applications. Application settings
Is there a pretty printing function in Clojure that would output data-structures like lists
In Clojure, I want to combine two lists to give a list of pairs,
I wrote a Clojure project using Leiningen and would now like to add the
I have a complex Clojure data structure that I would like to serialize -
I'm trying to implement sieve of Eratosthenes in Clojure. One approach I would like
I would like to set the fill-column in Emacs to 120 for Clojure-mode, but

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.