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?
#6
lst = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']
def new_element(item):
    return  item[0] if item[1] != 'not' else ' '.join(item)
new_list = [new_element(x) for x in zip(lst[:-1],lst[1:]) if x[0] !='not']

# ALTERNATIVE, NOT VERY PYTHONIC ONE-LINER
new_list2 = [item[0] if item[1] != 'not' else ' '.join(item)
             for item in zip(lst[:-1],lst[1:]) if item[0] !='not']

print new_list
print new_list2
Reply


Messages In This Thread
RE: How can I unite some elems in the list? - by buran - Jul-25-2017, 10:50 AM

Forum Jump:

User Panel Messages

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