[erlang-questions] Modify a list of tuples

Richard Carlsson <>
Thu Aug 14 23:36:23 CEST 2008


Dave Bryson wrote:
> What's the best way to modify a value for a given tuple in the list  
> below?
> 
> I have a large list with the following type of structure:
> 
> [ [{a,1},{b,2}], [{b,3},{a,4}] ]
> 
> I want to preserve the structure of the list and change a value on all  
> the tuples with a given key. So for example, change all the values on  
> the "b" tuple so the result would be:
> 
> [ [{a,1},{b,CHANGED}], [{b,CHANGED},{a,4}] ]
> 
> I've tried list comprehension but can't get it to preserve the rest of  
> the list.

Write a list comprehension that applies lists:keymap/3 on each
of the sublists.

    /Richard



More information about the erlang-questions mailing list