Python Forum
How can I unite some elems in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I unite some elems in the list?
#2
here's one way:
list_ = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']

newitem = ''
for n, item in enumerate(list_):
   if item == 'not' and n > 0:
       newitem = newitem + ' ' + list_[n-1]
print(newitem)
results:
Output:
is are
A better way would be to use a list comprehension
Reply


Messages In This Thread
RE: How can I unite some elems in the list? - by Larz60+ - Jul-23-2017, 10:45 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020