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

    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    How can i fill up an empty list using 'for' ?


    code:

    Code:
    #my failed attempt:
    import math
    mylist=[]
    i=0
    for item in range(len(mylist)):
    	mylist[i]=math.log(2,10)*i
    	i+=1
    problem:
    The idea is that i want to start adding new items to an empty list, i want to grow my list progressively.Later,when i understand the specific example, i will make a more general code by using input() instruction so that the user will input whatever data.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    Code:
    #   learn to use the documentation!!!
    
    #   docs.python.org
    
    #   >>> help(list)
    
    
    import math
    
    
    your_list=[]
    i=0
    for item in 'abcdef': # len(your_list) is 0.  The block of code within the loop would have exectuted 0 repetitions
        your_list.append(math.log(2,10)*i)  # append method of list
        i+=1
    
    
    constant = math.log(2,10)
    my_list = [constant*i for i in range(6)]  # list comprehension
    
    
    
    assert my_list == your_list
    Last edited by b49P23TIvg; October 24th, 2013 at 02:13 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by b49P23TIvg
    # learn to use the documentation!!!
    i need to buy myself a new pair of eyes

IMN logo majestic logo threadwatch logo seochat tools logo