Help with nested list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajasankar
    New Member
    • Jul 2009
    • 3

    Help with nested list

    Hi,

    I need help creating nested lists

    my code follows

    Code:
    test = ["a","b","c"]
    testtwo = ["e","f","g"]
    
    mo = []
    for i in range (0,len(test)):
        for j in range(0,len(testweb)):
             moa = test[i]+testweb[j]
             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
    Recognized Expert Specialist
    • Oct 2006
    • 2851

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

    Code:
    >>> test = ["a","b","c"]
    >>> testtwo = ["e","f","g"]
    >>> mo = {}
    >>> for i in test:
    ... 	for j in testtwo:
    ... 		mo.setdefault(i+j, [])
    ... 		
    []
    []
    []
    []
    []
    []
    []
    []
    []
    >>> mo
    {'be': [], 'bf': [], 'bg': [], 'ae': [], 'ag': [], 'af': [], 'cg': [], 'cf': [], 'ce': []}
    >>>

    Comment

    • rajasankar
      New Member
      • Jul 2009
      • 3

      #3
      Hi,

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

      Thanks again.


      Rajasankar

      Comment

      Working...