#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Multiple Loop construct


    I want to compact those two for-loop to one-line
    The first is a dict which contains list, the list inturn contain lists of single or multiple items

    Code:
    for chain in MainDict['filters']:
            for filter in chain:
                print('filter Name: {0}'.format(filter.filterName))
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481

    yes, the multiple comprehension confuses me a bit too.


    Code:
    '''
        for chain in MainDict['filters']:
            for filter in chain:
                print('filter Name: {0}'.format(filter.filterName))
    
    1st, it will be essentially a print statement/function call.
    2nd, new lines are involved.
         giving  print('\n'.join(SOMETHING))
    3rd: mild consideration required.
         (xxx for chain in MainDict['filters'] for filter in chain)
    
    '''
    
    
    
    
    print('\n'.join('filter Name: {0}'.format(filter.filterName) for chain in MainDict['filters'] for filter in chain))
    
    
    
    
    
    # correct, \textit{trust} me
    
    #for i in range(4):
    #    for j in 'abc':
    #	print(i,j)
    
    print('\n'.join('{} {}'.format(i,j) for i in range(4) for j in 'abc'))
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo