Python convert a list of strings in a nested list to list
Question: I have a variable called last_price which outputs:

    
    
                                  SPY
    Date                             
    2015-02-02 00:00:00+00:00  201.92
    

I want to extend last_price to a shape of 1000,1 using the following code:

    
    
    lp_list=[]
    lp_len=np.ones(1000)
    for o in lp_len:
        p=last_price
        lp_list.append(p)
    

but print lp_list outputs:

    
    
    [                              SPY
    Date                             
    2015-02-02 00:00:00+00:00  201.92,                               SPY
    Date                             
    2015-02-02 00:00:00+00:00  201.92,                               SPY
    Date                             
    2015-02-02 00:00:00+00:00  201.92
    etc...
    

Regardless of what my final end use is, how can I convert lp_list to a flat
list of the price string only (201.92)?

When I try to flatted using a list comprehension I get the following:

    
    
    >>flat_lp = [y for x in lp_list for y in x]
    >>print flat_lp
    ...['SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY',     'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY', 'SPY',
    etc...
    

Is there a way that I can create a flat list of just last_price variable with
output of 201.92, repeated 1000 times, such as in the below:

    
    
    >>print lp_list
    ...[201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92,
    etc...
    

Two things to consider:

1) last_price is a dataframe type:

    
    
    <class 'pandas.core.frame.DataFrame'>
    

2) The elements in lp_list need to be float type integer

Thanks


Answer: If you just want to repeat your price you can just use `itertool.repeat` , so
you dont need loops , you can split the string then pick the last element with
`[-1]` indexing :

    
    
    >>> s="""                              SPY
    ... Date                             
    ... 2015-02-02 00:00:00+00:00  201.92"""
    >>> s.split()
    ['SPY', 'Date', '2015-02-02', '00:00:00+00:00', '201.92']
    >>> from itertools import repeat
    >>> repeat(s.split()[-1],10)
    repeat('201.92', 10)
    >>> list(repeat(float(s.split()[-1]),10))
    [201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92, 201.92]
    

