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

后端 未结 10 1226
自闭症患者
自闭症患者 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:50

    _ is a standard placeholder name for ignored members in a for-loop and tuple assignment, e.g.

    ['' for _ in myList]
    
    [a+d for a, _, _, d, _ in fiveTuples]
    

    BTW your list could be written without list comprehension (assuming you want to make a list of immutable members like strings, integers etc.).

    [''] * len(myList)
    

提交回复
热议问题