How to modify list entries during for loop?

前端 未结 9 881
旧巷少年郎
旧巷少年郎 2020-11-22 01:56

Now I know that it is not safe to modify the list during an iterative looping. However, suppose I have a list of strings, and I want to strip the strings themselves. Does re

9条回答
  •  时光取名叫无心
    2020-11-22 02:08

    No you wouldn't alter the "content" of the list, if you could mutate strings that way. But in Python they are not mutable. Any string operation returns a new string.

    If you had a list of objects you knew were mutable, you could do this as long as you don't change the actual contents of the list.

    Thus you will need to do a map of some sort. If you use a generator expression it [the operation] will be done as you iterate and you will save memory.

提交回复
热议问题