[Tutor] Better way to insert items into a list
bob gailer
bgailer at gmail.com
Sat Dec 8 22:18:08 CET 2012
On 12/8/2012 2:39 PM, Mike wrote:
> 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.
a = 'ABCD'
c = 'OO'
tuple(sum([list(x) for x in zip(a,[c]*len(a))],[]))
This assumes that a can be just letters. Otherwise add the split('.')
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list