1

I have a list, temp1 = ['a','b','c','d','e','f']

I want the output as a new list new_list=['a.b.c.d.e.f']

I had tried this

def combine(temp1, lstart, lend):
    global y,z,x
    for w in temp1[lstart:lend-1]: 
        y=w+"."
        z=w
    for w in temp1[lend-1:lend]:
        z=w

for i in edge_names:
    temp1=(i.split('.'))
    print(temp1)
    right = len(temp1) - 2
    combine(temp1, 0, right)

but unable to get the desired result. Please help!

2 Answers 2

4

A simple solution would be to use the .join method

new_list = [".".join(temp1)]

This will give you the desired output of new_list = ["a.b.c.d.e.f"]

Sign up to request clarification or add additional context in comments.

Comments

0

You can also do this (but there are even better ways as pointed in other answers).

s=''  
for i in range(len(temp1)-1):
    s =  s + temp1[i] + '.'
if len(temp1) > 0:
    s =  s + temp1[-1]
newlist = [s]

4 Comments

It's insanity to me that this is the accepted answer. I mean, it works, but the readability of a join operation is infinitely better
@itypewithmyhands.: I apologize first of all for this. And I accept what you said. The other answer contains one of my upvotes. It's better.
No need to apologise, I was just flabbergasted enough to voice my comment, that's all. OP got what they wanted, and at the end of the day that's the goal here I guess
@itypewithmyhands.: Yes and no both. I was adding this answer to show that even if you know basics of coding you can realize something. But there are other things that python provides which are useful and efficient. One should know that. (But yes there is always a simple stupid way to get there at first try.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.