Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

having trouble with appending in nested lists in python

I am having trouble with appending to a nested list in python. It keeps giving me a final list with double the list size it should be.

When I loop through the second loop the first time it gives me an list with eleven elements which is correct. Therefore I try to create a nest loop where it appends those eleven elements as a list within a list. However, what it is in fact doing is just appending 11 elements twice (the first loop runs twice) so 22 elements and then giving me the same nest loop of 22 elements two times.

This is probably a dumb question but could you tell me how to get a nested list with two sublists of 11 elements?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

date = []
date2 = []

for i in event_subset_idx:
    print(i)
    picktime_matplotlib, picktime_utcdate, times_utc = apply_kurtosis(data_array)

    for k in range(0,len(picktime_utcdate)):
        print(k)
        ymd, hm, sec = extract_picktime_yearmonthday_hourmin_secs(picktime_utcdate[k])
        date.append(ymd)
date2.append(date)

>Solution :

Initialize date and append to date2 inside the first loop.

date2 = []

for i in event_subset_idx:
    print(i)
    picktime_matplotlib, picktime_utcdate, times_utc = apply_kurtosis(data_array)

    date = []
    for k in range(0,len(picktime_utcdate)):
        print(k)
        ymd, hm, sec = extract_picktime_yearmonthday_hourmin_secs(picktime_utcdate[k])
        date.append(ymd)
    date2.append(date)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading