'Iterate through list to create list of lists

I have two lists: list1 and list2 such that:

list1: [‘str0’,’str1’,str2’,etc.] of len() N
list2: [‘dataframe0’,’dataframe1’,’dataframe2’,dataframe3’,etc.] of len() N

(the two lists are of the same length)

What I want to do this this:

  • Use both lists combined (such that index 0, 1, 2, 3, etc. for the lists are linked). (so zip() within a for loop)

  • I want to use the associated index ‘strX’ in renaming the columns in ‘dataframeX’.

  • I want to then create a new list of lists such that:

    • ListA: drop index 0 from list1 and list2, but keep all other remaining objects
    • ListB: drop index 1 from list1 and list2, but keep all other remaining objects
    • ListC: drop index 2 from list1 and list2, but keep all other remaining objects
    • Etc.

so to create a list of lists where every list represents a copy of the original list with one object removed (sequentially).

  • Newlist [ListA, ListB, ListC, etc.]

I am having trouble performing this within for loops even with embedded enumerate function.

Any thoughts or guidance?

Here is the original code I was trying (which was clearly not working):

OV = []
for i, j in (zip(range(0,len(syms)), range(0,len(Results)))):
    z = Results.index(Results[j])
    w = Results
    c = Results.copy()
    x = w.index(z)
    c.pop(x)
    b = syms[i]
    a = syms
    u = syms.copy()
    s = a.index(b)
    u.pop(s)
    r = []
    for m, n in (zip(range(0,len(c)), range(0,len(u)))):
        y = c[m].copy()
        y.columns = [str(u[n]) + str(col) for col in y.columns]
        r.append(y)
    o = pd.concat(r, axis = 1)
    ov = pd.concat([o, outside], axis = 1)
    ove = pd.concat([z, ov], axis = 1)
    OV.append(ove)

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source