[Tutor] Better way to insert items into a list

Mike xchimeras at gmail.com
Sat Dec 8 20:39:49 CET 2012


Hello everyone,

I was wondering if someone could show me a better way to achieve what
I am trying to do.  Here is my test code:

    d=[]
    c="00"
    a="A,B,C,D"
    b=a.split(',')
    for item in b:
        d.append(item)
        d.append(c)
    print tuple(d)

Basically what I want to end up with is a tuple that looks like this:
("A","00","B","00","C","00")...

As you can see I want to insert 00 in-between each element in the
list.  I believe this could be done using a list comprehension?

I realize I achieved my goal, but I was wondering what other
alternates could be done to achieve the same results.

Thanks in advance for your assistance.

Mike.


More information about the Tutor mailing list