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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:17:35+00:00 2026-05-13T08:17:35+00:00

I have this deeply nested list (list of lists) and I want to replace

  • 0

I have this deeply nested list (list of lists) and I want to replace a single arbitrary element in the list. How can I do this ? (The built-in replace might replace many occurrences while I need to replace only one element.)

  • 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-13T08:17:35+00:00Added an answer on May 13, 2026 at 8:17 am

    As everyone else already said, using lists is really not a good idea if you need to do this kind of thing. Random access is what vectors are made for. assoc-in does this efficiently. With lists you can’t get away from recursing down into the sublists and replacing most of them with altered versions of themselves all the way back up to the top.

    This code will do it though, albeit inefficiently and clumsily. Borrowing from dermatthias:

    (defn replace-in-list [coll n x]
      (concat (take n coll) (list x) (nthnext coll (inc n))))
    
    (defn replace-in-sublist [coll ns x]
      (if (seq ns)
        (let [sublist (nth coll (first ns))]
          (replace-in-list coll
                           (first ns)
                           (replace-in-sublist sublist (rest ns) x)))
        x))
    

    Usage:

    user> (def x '(0 1 2 (0 1 (0 1 2) 3 4 (0 1 2))))
    #'user/x
    user> (replace-in-sublist x [3 2 0] :foo) 
    (0 1 2 (0 1 (:foo 1 2) 3 4 (0 1 2)))
    user> (replace-in-sublist x [3 2] :foo) 
    (0 1 2 (0 1 :foo 3 4 (0 1 2)))
    user> (replace-in-sublist x [3 5 1] '(:foo :bar)) 
    (0 1 2 (0 1 (0 1 2) 3 4 (0 (:foo :bar) 2)))
    

    You’ll get IndexOutOfBoundsException if you give any n greater than the length of a sublist. It’s also not tail-recursive. It’s also not idiomatic because good Clojure code shies away from using lists for everything. It’s horrible. I’d probably use mutable Java arrays before I used this. I think you get the idea.

    Edit

    Reasons why lists are worse than vectors in this case:

    user> (time
           (let [x '(0 1 2 (0 1 (0 1 2) 3 4 (0 1 2)))]               ;'
             (dotimes [_ 1e6] (replace-in-sublist x [3 2 0] :foo))))
    "Elapsed time: 5201.110134 msecs"
    nil
    user> (time
           (let [x [0 1 2 [0 1 [0 1 2] 3 4 [0 1 2]]]]
             (dotimes [_ 1e6] (assoc-in x [3 2 0] :foo))))
    "Elapsed time: 2925.318122 msecs"
    nil
    

    You also don’t have to write assoc-in yourself, it already exists. Look at the implementation for assoc-in sometime; it’s simple and straightforward (compared to the list version) thanks to vectors giving efficient and easy random access by index, via get.

    You also don’t have to quote vectors like you have to quote lists. Lists in Clojure strongly imply “I’m calling a function or macro here”.

    Vectors (and maps, sets etc.) can be traversed via seqs. You can transparently use vectors in list-like ways, so why not use vectors and have the best of both worlds?

    Vectors also stand out visually. Clojure code is less of a huge blob of parens than other Lisps thanks to widespread use of [] and {}. Some people find this annoying, I find it makes things easier to read. (My editor syntax-highlights (), [] and {} differently which helps even more.)

    Some instances I’d use a list for data:

    1. If I have an ordered data structure that needs to grow from the front, that I’m never going to need random-access to
    2. Building a seq “by hand”, as via lazy-seq
    3. Writing a macro, which needs to return code as data
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested unordered list representing a tree hierarchy. There can be many
Let's say I have some deeply nested markup that I want to target with
I have this little code which lists all matches of items that contains the
I have this problem, in a form I have a list of customers that
I have list items in an unordered list that when double clicked, can be
Suppose we have to use in some function deeply nested pointer very extensively: function
Thanks in advance for your time. I have a deeply nested XML whose XSLT
I have some deeply randomly nested dictionary as follows. {'CompilationStatistics': {'CodeGeneration': {'EndTime': '2010-04-21T14:03:11', 'StartTime':
I have a page with deeply-nested HTML elements generated from a framework. I would
I have a deeply nested partial view, where I iterate over a collection of

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.