# How to edit outside variables in mapping function

**URL:** <https://discuss.huggingface.co/t/how-to-edit-outside-variables-in-mapping-function/102221>\
**Category:** Beginners\
**Created:** [August 13, 2024, 6:34pm UTC](https://discuss.huggingface.co/t/how-to-edit-outside-variables-in-mapping-function/102221 "2024-08-13T18:34:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CadenHE](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/cadenhe/32/28211_2.png) [@CadenHE](https://discuss.huggingface.co/u/CadenHE)\
**Post date:** [August 13, 2024, 6:34pm UTC](https://discuss.huggingface.co/t/how-to-edit-outside-variables-in-mapping-function/102221/1 "2024-08-13T18:34:14Z")

</div>

I am trying to find a workaround for [my issue](https://discuss.huggingface.co/t/dataset-map-raises-value-error-when-mapping-list-to-dict-like-class/101554) and to do so I would like to continue using the mapping function as I have a very large dataset (much bigger than the one in the linked post). I would like to store the multisets in a list of dicts like so:

```auto
list_data = []
def _multisetify(code_file):
    list_data.append(Multiset(code_file["tokens"]))
    return code_file
data.shuffle() # just to make the tqdm output more accurate
print(data.map(_multisetify, num_proc=32))

```

However this just leaves list\_data empty? Does anybody know someway I would be able to do this using the map() function or someway I can do it as quickly as the map function?

---

<div class="post-metadata">

**Author:** ![mikehemberger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mikehemberger/32/28013_2.png) [@mikehemberger](https://discuss.huggingface.co/u/mikehemberger)\
**Post date:** [August 13, 2024, 6:53pm UTC](https://discuss.huggingface.co/t/how-to-edit-outside-variables-in-mapping-function/102221/2 "2024-08-13T18:53:06Z")

</div>

Hi again,  
I suspect that this here and your other issue have the same solution.  
[Issue](https://discuss.huggingface.co/t/dataset-map-raises-value-error-when-mapping-list-to-dict-like-class/101554)

---

<div class="post-metadata">

**Author:** ![CadenHE](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/cadenhe/32/28211_2.png) [@CadenHE](https://discuss.huggingface.co/u/CadenHE)\
**Post date:** [August 13, 2024, 7:44pm UTC](https://discuss.huggingface.co/t/how-to-edit-outside-variables-in-mapping-function/102221/3 "2024-08-13T19:44:46Z")

</div>

It doesn’t raise any error in this one it just doesn’t allow me to edit or create data that can be accessed outside of the function, like if I made a variable in \_multisetify() it wouldn’t be able to be accessed outside (as it should), but if I make a variable scoped outside of \_multisetify() and edit it inside (with append) then it changes inside the function (I checked), but does not get changed outside \_multisetify().

---

<div class="post-metadata">

**Author:** ![mikehemberger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mikehemberger/32/28013_2.png) [@mikehemberger](https://discuss.huggingface.co/u/mikehemberger)\
**Post date:** [August 13, 2024, 8:37pm UTC](https://discuss.huggingface.co/t/how-to-edit-outside-variables-in-mapping-function/102221/4 "2024-08-13T20:37:10Z")

</div>

In this example case you would have to also return the list\_data

> return code\_file, list\_data

But that doesn’t solve your problem.  
To help debug this issue you might just run a loop over the data.  
Sorry, hope someone can help,  
M
