By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Help with nested list

P: 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
Jul 2 '09 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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. >>> 
Jul 2 '09 #2

P: 3
Hi,

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

Thanks again.


Rajasankar
Jul 3 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.