I have an array with 8 items like the below array (dummy data), I only want the first 6 items. I can’t seem to get .join correct.
["MR_L: ", 20, "Time: ", "10:00", "Email: ", "Testing@test", "Telephone: ", 0002411212]
I want the out come to be in a string:
"MR_L: 20
Time: 10:00
Email: Testing@test"
I’ve tried a for loop like: for index in range (0, 5, 2) then use index -1. It is important this is all in one string as I am using a function that can only take a single string as an argument. When I use join I get a memory error.
>Solution :
If the integer value(s) in your list are valid then you could do this:
mylist = ["MR_L: ", 20, "Time: ", "10:00", "Email: ", "Testing@test", "Telephone: ", 2411212]
parts = []
for i in range(0, min(6, len(mylist)), 2):
parts.append('{}{}'.format(*mylist[i:i+2]))
print(*parts, sep='\n')
Output:
MR_L: 20
Time: 10:00
Email: Testing@test