Advertisement
Programmin-in-Python

Printing a concatenated string with 'n' adjacent partners

Nov 4th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. myList = ["This", "is", "a",  "great", "list"]
  2. n = 2
  3. tempN = n
  4.  
  5. res = []
  6.  
  7. for i in range(len(myList)-n+1):
  8.     start, tempRes = myList[i], ""
  9.  
  10.     for j in range(i, len(myList)):
  11.         if tempN>0:
  12.             if tempN == 1:
  13.                 tempRes += f"{myList[j]}"
  14.             else:
  15.                 tempRes += f"{myList[j]} "
  16.  
  17.             tempN -= 1
  18.     else:
  19.         res.append(tempRes)
  20.         tempN = n
  21.  
  22. print(res)
  23.  
  24. ##Output : ['This is', 'is a', 'a great', 'great list']
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement