1

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:

I4 = [[(0, 0), (1, 0)],[(0, 0), (0, -1)],[(1, 0), (1, -1)],[(0, -1), (1, -1)]] 
1
  • 2
    You're initializing I4 each time you enter the loop. Initialize it to [] before the loop and append to it within the loop. Commented Jul 1, 2022 at 15:48

2 Answers 2

3

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)]] 
Sign up to request clarification or add additional context in comments.

Comments

2

Here is a one liner

I4 = [[(j, -i), (l, -k)] for (i, j), (k, l) in I3] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.