having trouble with appending in nested lists in python

Question:

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?

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)
Asked By: user2558894

||

Answers:

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)
Answered By: Barmar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.