concatenating 2 items at a time in a python list

All other Source.Python topics and issues.
k11
Junior Member
Posts: 1
Joined: Wed Oct 21, 2020 7:43 am

concatenating 2 items at a time in a python list

Postby k11 » Wed Oct 21, 2020 7:46 am

I have a list named data having the items shown as below.
a229f7d1148c27d5ff46cbf506f92a9e
3c73dc22ddaafa58346cc5241a78d509
c5ebe3eff60ef972fdd9a9d5e4762227
1c720ec8c2615529e1500d77020a1dc2
63d8e40d1c5aabbee5cf35a13a95b089
0994bcd4f5722e3ae8620a483b83abbf
06f628503dddc37956e586b9e537b3ae
a229f7d1148c27d5ff46cbf506f92a9e
3c73dc22ddaafa58346cc5241a78d509
f6ebd290be34c47ffc84f0a4f123112a
c5ebe3eff60ef972fdd9a9d5e4762227
1c720ec8c2615529e1500d77020a1dc2

I want to concatenate 2 items at a time for the entire list and copy that to an output file as below:
data[index+ 1] + data[index] so that i get the following items
3c73dc22ddaafa58346cc5241a78d509a229f7d1148c27d5ff46cbf506f92a9e
1c720ec8c2615529e1500d77020a1dc2c5ebe3eff60ef972fdd9a9d5e4762227
and so on till the end of the list.

ind = 0
while ind < len(data):
for i in range(int(len(data)/2)):
new_file.write("".join(data[(ind + 1): ind]))
new_file.write("\n")
ind += 2

But i am not getting the desired output. What am i doing wrong in the while loop?
User avatar
L'In20Cible
Project Leader
Posts: 1381
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: concatenating 2 items at a time in a python list

Postby L'In20Cible » Wed Oct 21, 2020 11:21 am

Syntax: Select all

for i in range(1, len(data), 2):
new_file.write(f'{data[i] + data[i - 1]}\n')
DeaD_EyE
Junior Member
Posts: 28
Joined: Wed Jan 08, 2014 10:32 am

Re: concatenating 2 items at a time in a python list

Postby DeaD_EyE » Fri Oct 23, 2020 9:32 am


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests