Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with nested list

Newbie
 
Join Date: Jul 2009
Posts: 3
#1: Jul 2 '09
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

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#2: Jul 2 '09

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. >>> 
Newbie
 
Join Date: Jul 2009
Posts: 3
#3: Jul 3 '09

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