[Tutor] String made of list elements' nth characters?

Kent Johnson kent37 at tds.net
Sun Apr 20 18:27:45 CEST 2008


R. Alan Monroe wrote:

> def checksolution(sol):
>     maybes = []
>     big = ''.join(solution)
>     for x in range(len(sol)):
>         maybes.append( big[x::6] )
>     print maybes
> 
> solution=['abject','poetry','keypad']
> checksolution(solution)
> 
> ['apk', 'boe', 'jey']

That's nice. Why do you use len(sol) instead of len(sol[0])? You only 
print the first three items. Here is another way to do it:

In [8]: solution=['abject','poetry','keypad']
In [9]: [''.join(x) for x in zip(*solution)]
Out[9]: ['apk', 'boe', 'jey', 'etp', 'cra', 'tyd']

Kent


More information about the Tutor mailing list