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

Appending lists to a list

I have a list I3. I am performing an index operation through I4 and appending based on the length of I3. The current and expected outputs are presented.

I3 = [[(0, 0), (0, 1)], [(0, 0), (1, 0)], [(0, 1), (1, 1)], [(1, 0), (1, 1)]]

for t in range(0,len(I3)):
    I4 = [(j, -i) for i, j in I3[t]]
    I4.append(I4)
    print("I4 =",I4)

The current output is:

I4 = [(0, 0), (1, 0), [...]]
I4 = [(0, 0), (0, -1), [...]]
I4 = [(1, 0), (1, -1), [...]]
I4 = [(0, -1), (1, -1), [...]]

The expected output is:

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

I4 = [[(0, 0), (1, 0)],[(0, 0), (0, -1)],[(1, 0), (1, -1)],[(0, -1), (1, -1)]]

>Solution :

Note I4.append(I4) and I4 = [(0, 0), (1, 0), [...]]. You’re appending a list to the end of itself. That’s recursive. Try to append it to an empty list.

I3 = [[(0, 0), (0, 1)], [(0, 0), (1, 0)], [(0, 1), (1, 1)], [(1, 0), (1, 1)]]

temp = []
for t in range(0,len(I3)):
    I4 = [(j, -i) for i, j in I3[t]]
    temp.append(I4)
    print("I4 =",temp)

results in

I4 = [[(0, 0), (1, 0)]]
I4 = [[(0, 0), (1, 0)], [(0, 0), (0, -1)]]
I4 = [[(0, 0), (1, 0)], [(0, 0), (0, -1)], [(1, 0), (1, -1)]]
I4 = [[(0, 0), (1, 0)], [(0, 0), (0, -1)], [(1, 0), (1, -1)], [(0, -1), (1, -1)]]
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