Oct-21-2020, 09:19 AM
(Oct-21-2020, 08:21 AM)bowlofred Wrote: First problem is that your for loop seems extraneous. You're not using the index variable later.I used the following code and it works..kindly check and let me know if its correct.
Your while loop is close, but you're slicing it incorrectly. You want the later value first, you've writtendata[ind+1:ind]
. But since the default step is 1 (a positive number), the slice never works. Better would be to write them as two different accesses:data[ind+1], data[ind]
and combine them.
Tweaking it...
import string data = string.ascii_lowercase[:8] ind = 0 while ind < len(data): print("".join(data[ind+1] + data[ind])) ind += 2
Output:ba dc fe hg
for i in range(0, len(data), 2):
next = data[i+1] + data[i]
Expected_z.append(next)