assoc & subst are the key functions here - similar to how you might manipulate the DXF data returned by entget. You are working with an association list wherein the first element of each sublist represents a key, and the tail of the list represents the data associated with that key. You can use assoc to test whether a sublist with a given key exists, and then use subst to substitute the sublist with a modified version within the main list.
For example - say you start with the following list:
_$ (setq lst '(("2" "b") ("1" "a")))
(("2" "b") ("1" "a"))
And you have the following new values:
_$ (setq key "2" val "c")
You can test whether the key exists using assoc:
_$ (setq old (assoc key lst))
("2" "b")
Since it does, you can modify it using the value returned by assoc:
_$ (setq new (append old (list val)))
("2" "b" "c")
And finally, you can substitute it into the main list using subst:
_$ (setq lst (subst new old lst))
(("2" "b" "c") ("1" "a"))
If the key does not exist, you can use cons to push it onto the list:
_$ (setq key "3" val "c")
_$ (assoc key lst)
nil
_$ (setq lst (cons (list key val) lst))
(("3" "c") ("2" "b" "c") ("1" "a"))
Putting this all together with an if statement to branch accordingly, we can write a function such as the following:
(defun addtolist ( key val lst / old )
(if (setq old (assoc key lst))
(subst (append old (list val)) old lst)
(cons (list key val) lst)
)
)
_$ (setq lst '(("2" "b") ("1" "a")))
(("2" "b") ("1" "a"))
_$ (setq lst (addtolist "2" "c" lst))
(("2" "b" "c") ("1" "a"))
_$ (setq lst (addtolist "3" "d" lst))
(("3" "d") ("2" "b" "c") ("1" "a"))