Search code examples
pythonsubstitutionhy

Substitution of list/dictionary element by the index in Hy


Is there any way to substitute a value for an element of list or dictionary by the index in Hy? The nth function seems not corresponds to Python's square bracket.

I had expected like the following translation.

(setv lst [1 2 3])
(setv (nth lst 1) 20)

lst=[1, 2, 3]
lst[1]=20

Solution

  • According to the documentation, you have to use the assoc function to set a value at a specific index in a list. As such, your code should be:

    (assoc lst 1 20)
    

    This should give the desired result.