Python - Importing strings into a list, into another list :)
Question: Basically I want to read strings from a text file, put them in lists three by
three, and then put all those three by three lists into another list. Actually
let me explain it better :)

Text file (just an example, I can structure it however I want):

    
    
    party    
    sleep  
    study    
    --------   
    party   
    sleep  
    sleep    
    -----   
    study  
    sleep  
    party   
    ---------
    

etc

From this, I want Python to create a list that looks like this:

    
    
    List1 = [['party','sleep','study'],['party','sleep','sleep'],['study','sleep','party']etc]
    

But it's super hard. I was experimenting with something like:

    
    
    test2 = open('test2.txt','r')
    List=[]
    
    for line in 'test2.txt':
        a = test2.readline()
        a = a.replace("\n","")
        List.append(a)
        print(List)
    

But this just does horrible horrible things. How to achieve this?


Answer: **If you want to group the data in size of 3**. Assumes your data in the text
file is not grouped by any separator.

You need to read the file, sequentially and create a list. To group it you can
use any of the known [grouper
algorithms](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-
into-evenly-sized-chunks-in-python)

    
    
    from itertools import izip, imap
    with open("test.txt") as fin:
        data = list(imap(list, izip(*[imap(str.strip, fin)]*3)))
    
    pprint.pprint(data)
    [['party', 'sleep', 'study'],
     ['party', 'sleep', 'sleep'],
     ['study', 'sleep', 'party']]
    

_Steps of Execution_

  1. Create a Context Manager with the file object.
  2. Strip each line. (Remove newline)
  3. Using zip on the iterator list of size 3, ensures that the items are grouped as tuples of three items
  4. Convert tuples to list
  5. Convert the generator expression to a list.

Considering all are generator expressions, its done on a single iteration.

**Instead, if your data is separated and grouped by a delimiter`------` you
can use the `itertools.groupby` solution**

    
    
    from itertools import imap, groupby
    class Key(object):
        def __init__(self, sep):
            self.sep = sep
            self.count = 0
        def __call__(self, line):
            if line == self.sep:    self.count += 1
            return self.count
    
    
    with open("test.txt") as fin:
        data = [[e for e in v if "----------" not in e]
            for k, v in groupby(imap(str.strip, fin), key = Key("----------"))]
    
    
    pprint.pprint(data)
    [['party', 'sleep', 'study'],
     ['party', 'sleep', 'sleep'],
     ['study', 'sleep', 'party']]
    

_Steps of Execution_

  1. Create a Key Class, to increase a counter when ever the separator is encountered. The function call spits out the counter every-time its called apart from conditionally increasing it. 
  2. Create a Context Manager with the file object.
  3. Strip each line. (Remove newline)
  4. Group the data using `itertools.groupby` and using your custom key
  5. Remove the separator from the grouped data and create a list of the groups.

