How can I get around declaring an unused variable in a for loop?

后端 未结 10 1224
自闭症患者
自闭症患者 2020-11-27 16:03

If I have a list comprehension (for example) like this:

[\'\' for x in myList]

Effectively making a new list that has an empty string for e

10条回答
  •  渐次进展
    2020-11-27 16:46

    The generator objects don't actually use the variables. So something like

    list(('' for x in myList))
    

    should do the trick. Note that x is not defined as a variable outside of the generator comprehension.

提交回复
热议问题