Connecting Tech Pros Worldwide Help | Site Map

Help with nested list

  #1  
Old July 2nd, 2009, 12:34 PM
Newbie
 
Join Date: Jul 2009
Posts: 3
Hi,

I need help creating nested lists

my code follows

Expand|Select|Wrap|Line Numbers
  1. test = ["a","b","c"]
  2. testtwo = ["e","f","g"]
  3.  
  4. mo = []
  5. for i in range (0,len(test)):
  6.     for j in range(0,len(testweb)):
  7.          moa = test[i]+testweb[j]
  8.          mo.append[moa]
I want final output like this

mo[ae[],af[],ag[],be[], ....... cg[]] instead of mo[ae,af,ag,be, ....... cg]

How to get this??? I've searched in this forum and couldn't find an answer.

Thanks,
Rajasankar
  #2  
Old July 2nd, 2009, 03:35 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

re: Help with nested list


Your desired output is invalid. The brackets '[]' represent a slice object. What you are wanting is similar to:
Expand|Select|Wrap|Line Numbers
  1. >>> s = slice(10,20)
  2. >>> s.indices(15)
  3. (10, 15, 1)
  4. >>> s.indices()
  5. Traceback (most recent call last):
  6.   File "<interactive input>", line 1, in ?
  7. TypeError: indices() takes exactly one argument (0 given)
  8. >>> 
You may want a dictionary with empty lists as values.

Expand|Select|Wrap|Line Numbers
  1. >>> test = ["a","b","c"]
  2. >>> testtwo = ["e","f","g"]
  3. >>> mo = {}
  4. >>> for i in test:
  5. ...     for j in testtwo:
  6. ...         mo.setdefault(i+j, [])
  7. ...         
  8. []
  9. []
  10. []
  11. []
  12. []
  13. []
  14. []
  15. []
  16. []
  17. >>> mo
  18. {'be': [], 'bf': [], 'bg': [], 'ae': [], 'ag': [], 'af': [], 'cg': [], 'cf': [], 'ce': []}
  19. >>> 
  #3  
Old July 3rd, 2009, 03:29 AM
Newbie
 
Join Date: Jul 2009
Posts: 3

re: Help with nested list


Hi,

Thanks for this info. I used the dict in Python and it worked.

Thanks again.


Rajasankar
Reply

Tags
list, python, python list


Similar Threads
Thread Thread Starter Forum Replies Last Post
nested list comprehension and if clauses Jyotirmoy Bhattacharya answers 5 June 28th, 2007 07:25 AM
Bug with nested repeaters. Item events are called twice per item =?Utf-8?B?SmFtZXMgR2V1cnRz?= answers 4 March 28th, 2007 02:55 AM
help with 'left-right' cdecl rule James Brown [MVP] answers 6 February 14th, 2006 04:25 PM
Problem with nested Datalists TheHach answers 1 February 1st, 2006 03:15 PM