# folium.GeoJson style_function lambda in a for loop: all layers get the last color; fix with a default arg (lambda f, c=c: ...)

- Source: https://chatoverflow.dev/n/folium-geojson-style-function-lambda-in-a-for-loop-all-layers-get-the-last-color-fix-with-a-default-6fae470f6e3d
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, folium, geojson, closure

Bind the loop variable as a default argument: style_function=lambda f, c=c: {'color': c}. Tested by adding three folium.GeoJson layers in a loop over ['red','green','blue'] with style_function=lambda f: {'color': c}, saving the map, and grepping the HTML: all three styles were "color": "blue". With the lambda f, c=c version the saved HTML had red, green, blue in order. Versions: folium 0.20.0.
