I have a list and I'm trying to append a series of new elements to this list in a certain order: Day, IDs (Array), Month, Year. I'm able to add these new elements to the list just fine, however, there are some redundant brackets that show up. Here's an example of the output (see extra []s towards the end of this list):
[['Saturday'], array([[1, 2, 5, 3]]), ['August'], [1998], ['Sunday'], array([[7, 1, 3, 4]]), ['May'], [1996], [['Monday'], array([[1, 2, 3, 4]]), ['April'], [1996]], [['Wednesday'], array([[5, 6, 7, 8]]), ['June'], [2001]]]
In an attempt to eliminate those, I tried to extend my existing list by accessing the inner list with an index (see line my_list.extend(temp[0]) below, but doing this does not append the entiretemp list. The output of this is as follows:
[['Saturday'], array([[1, 2, 5, 3]]), ['August'], [1998], ['Sunday'], array([[7, 1, 3, 4]]), ['May'], [1996], ['Monday'], array([[1, 2, 3, 4]]), ['April'], [1996]]
The following is a snippet of my code:
import numpy as np
my_list = [['Saturday'], np.array([[1, 2, 5, 3]]), ['August'], [1998], ['Sunday'], np.array([[7, 1, 3, 4]]), ['May'], [1996]]
# Note: The lists below could be of any length; not always 2. For example, there could be [['Monday'], ['Tuesday'], ['Wednesday']] and the associated IDs/Months/Years would be of the same length too.
Days = [['Monday'], ['Wednesday']]
IDs = [np.array([[1, 2, 3, 4]]), np.array([[5, 6, 7, 8]])]
Months = [['April'], ['June']]
Years = [[1996], [2001]]
temp = Days, IDs, Months, Years
temp = [list(t) for t in zip(*temp)]
my_list.extend(temp)
print (my_list)
How do I eliminate the extra [ ]s so as to achieve my expected output?
After a few more tries, I finally figured it out. I just flattened my list and it got rid of the redundant square brackets. Here is the updated code:
Output: