Create a new list from a list when a certain condition is met

后端 未结 4 1046
迷失自我
迷失自我 2020-12-14 20:43

I want to make a new list from another list of words; when a certain condition of the word is met. In this case I want to add all words that have the length of 9 to a new li

相关标签:
4条回答
  • 2020-12-14 21:14

    try this:

    newlist = [word for word in words if len(word) == 9]
    
    0 讨论(0)
  • 2020-12-14 21:20

    Sorry, realized you wanted length, 9, not length 9 or greater.

    newlist = [word for word in words if len(word) == 9]
    
    0 讨论(0)
  • 2020-12-14 21:28

    Try this:

    list= [list_word for list_word in words if len(list_word) == 1]
    
    0 讨论(0)
  • 2020-12-14 21:29

    Try:

    newlist = []
    for item in resultVital:
        if len(item) == 9:
            newlist.append(item)
    0 讨论(0)
提交回复
热议问题