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

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Cannot for the life of me figure out this...


    i want my code to get an input of a listoflists, and return it with all the internal lists added a 0 in front of it, e.g

    lets use t for listoflists
    addZero([[1,2,3][2,2,3],[3,2,3]])

    >> [[0,1,2,3][0,2,2,3],[0,3,2,3]]

    my code:

    Code:
    def addZero(t):
        for i in range (1, len(t)):
            t[i].insert(0,0)
    
            return t

    not sure what im doing wrong... please enlighten me and thank you very much!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Originally Posted by mrhlth
    i want my code to get an input of a listoflists, and return it with all the internal lists added a 0 in front of it,
    Hi,

    There is code indent problem in your code, the return statement has been put inside the for scope where it should be out of it Also the range should start from 0.

    Code:
    def addZero(p_listOfLists):
        for counter in range(0, len(p_listOfLists)):
            p_listOfLists[counter].insert(0,0)
            
        return p_listOfLists
            
    
    myList = [[1,2,3],[2,2,3],[3,2,3]]
    
    myList = addZero(myList)
    
    print myList
    Another way of writing

    Code:
    def addZero(p_listOfLists):
        for subList in p_listOfLists:
            subList.insert(0,0)
    
    myList = [[1,2,3],[2,2,3],[3,2,3]]
    
    addZero(myList)
    
    print myList
    You can also try this
    Code:
    >>> mylist = [[1,2,3],[2,2,3],[3,2,3]]
    >>> 
    >>> mylist = [[0]+subList for subList in mylist]
    >>> 
    >>> mylist
    [[0, 1, 2, 3], [0, 2, 2, 3], [0, 3, 2, 3]]
    >>>
    Last edited by dariyoosh; April 24th, 2013 at 05:19 AM.
    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by dariyoosh
    Hi,

    There is code indent problem in your code, the retuen statement has been put inside the for scope where it should be out of it Also the range should start from 0.

    Code:
    def addZero(p_listOfLists):
        for counter in range(0, len(p_listOfLists)):
            p_listOfLists[counter].insert(0,0)
            
        return p_listOfLists
            
    
    myList = [[1,2,3],[2,2,3],[3,2,3]]
    
    myList = addZero(myList)
    
    print myList
    Another way of writing

    Code:
    def addZero(p_listOfLists):
        for subList in p_listOfLists:
            subList.insert(0,0)
    
    myList = [[1,2,3],[2,2,3],[3,2,3]]
    
    addZero(myList)
    
    print myList
    You can also try this
    Code:
    >>> mylist = [[1,2,3],[2,2,3],[3,2,3]]
    >>> 
    >>> mylist = [[0]+subList for subList in mylist]
    >>> 
    >>> mylist
    [[0, 1, 2, 3], [0, 2, 2, 3], [0, 3, 2, 3]]
    >>>
    thank you very much! it worked for me and thanks for all the variations of answers!

IMN logo majestic logo threadwatch logo seochat tools logo