Connecting Tech Pros Worldwide Help | Site Map

very simple string to list function

Newbie
 
Join Date: Feb 2009
Posts: 31
#1: Mar 16 '09
the question that im wondering is how do i split into one list of str, say "123456789" into for every third value

so the output would be ['123' , '456' , '789']

thanks
Newbie
 
Join Date: Feb 2009
Posts: 31
#2: Mar 16 '09

re: very simple string to list function


solved:) the problem
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#3: Mar 16 '09

re: very simple string to list function


I am glad you figured it out. Here's what I was going to suggest:
Expand|Select|Wrap|Line Numbers
  1. >>> s = "123456789"
  2. >>> [s[i:i+3] for i in range(0, len(s), 3)]
  3. ['123', '456', '789']
  4. >>> 
Reply