-2

I'm new to python.

I'm trying to create another list from a big one just with 3 elements of that list at a time.

I'm trying this:

my_list = ['test1,test2,test3','test4,test5,test6','test7,test8,test9','test10,test11,test12']
new_three = []
for i in my_list:    
    item = my_list[int(i):3]
    new_three.append(item)

    # here I'll write a file with these 3 elements. Next iteration I will write the next three ones, and so on...

I'm getting this error:

item = my_list[int(i):3]
ValueError: invalid literal for int() with base 10: 'test1,test2,test3'

I also tried:

from itertools import islice
for i in my_list:
    new_three.append(islice(my_list,int(i),3))

Got the same error. I cannot figure out what I'm doing wrong.

EDIT:

After many tries with help here, I could make it.

listrange = []
for i in range(len(li)/3 + 1):
    item = li[i*3:(i*3)+3]
    listrange.append(item)
  • 2
    What's the expected output? – nalzok Aug 09 '16 at 14:03
  • Related: [Loop over a list of string using split](http://stackoverflow.com/questions/22307844/python-loop-over-a-list-of-string-and-using-split?noredirect=1&lq=1) – LinkBerest Aug 09 '16 at 14:31
  • 2
    When using a for loop, python will iterate over list *members*, not indices, as evidenced by your error message. When you say 3 elements of the list at a time, do you mean 3 strings at a time from my_list ? – Kevin M Granger Aug 09 '16 at 14:38
  • What Kevin said. The `my_list` given above only contains 4 elements. – PM 2Ring Aug 09 '16 at 14:40
  • You may find the answers here helpful: http://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks Also check the linked pages. – PM 2Ring Aug 09 '16 at 14:59

1 Answers1

0

Is this what you meant?

my_list = ['test1,test2,test3','test4,test5,test6','test7,test8,test9','test10,test11,test12']
for item in my_list:
    print "this is one item from the list :", item    
    list_of_things = item.split(',')
    print "make a list with split on comma:", list_of_things
    # you can write list_of_things to disk here
    print "--------------------------------"

In response to comments, if you want to generate a whole new list with the comma separated strings transformed into sublists, that is a list comprehension:

new_list = [item.split(',') for item in my_list]

And to split it up into groups of three items from the original list, see the answer linked in comments by PM 2Ring, What is the most "pythonic" way to iterate over a list in chunks?

I have adapted that to your specific case here:

my_list = ['test1,test2,test3','test4,test5,test6','test7,test8,test9','test10,test11,test12']

for i in xrange(0, len(my_list), 3):
    # get the next three items from my_list
    my_list_segment = my_list[i:i+3]

    # here is an example of making a new list with those three
    new_list = [item.split(',') for item in my_list]
    print "three items from original list, with string split into sublist"
    print my_list_segment
    print "-------------------------------------------------------------"

    # here is a more practical use of the three items, if you are writing separate files for each three
    filename_this_segment = 'temp' # make up a filename, possibly using i/3+1 in the name
    with open(filename_this_segment, 'w') as f:
        for item in my_list_segment:
            list_of_things = item.split(',')
            for thing in list_of_things:
                # obviously you'll want to format the file somehow, but that's beyond the scope of this question
                f.write(thing)
Community
  • 1
  • 1
Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • #first iteration `[['test1', 'test2', 'test3'],['test4', 'test5', 'test6'],['test7', 'test8', 'test9']]` #second iteration `[['test10', 'test11', 'test12'],[# nextt group of three],[# next another group of three]]` # third iteraction `[[# next other group of three],[# next other group of three],[# next other group of three]]` # and son on... – Fabricio Carboni Aug 10 '16 at 14:42
  • Hold on, I think you want the question linked in comments by PM 2Ring, in addition to using split(','). Updating ... – Kenny Ostrom Aug 10 '16 at 15:00