0

I have a list inside a loop for e.g

A=[25,45,34,....87]

in the next iteration A should be

A=[[25,32],[45,13],[34,65],....[87,54]]

in the next iteration A should be

A=[[25,32,44],[45,13,67],[34,65,89],....[87,54,42]]

and so on.How can i do that?is it possible?The code i am working on is

    s=0
    e=25
    for i in range(0,4800):
        if not m_list_l:
            m_list_l.append(max(gray_sum[s:e]))
        m_list_l[i].append(max(gray_sum[s:e]))
        s+=25
        e+=25

But this give me Error as

m_list_l[i].append(max(gray_sum[s:e]))
AttributeError: 'int' object has no attribute 'append'
9
  • Where are these extra elements coming from? Commented Dec 16, 2016 at 5:58
  • What should m_list_l[i].append() do before there are nested lists? Commented Dec 16, 2016 at 5:58
  • @pvg gray_sum is another large list Commented Dec 16, 2016 at 5:59
  • @Natecat i declared m_list_l as an empty list in the beginning of the program Commented Dec 16, 2016 at 6:02
  • It's not clear where the extra elements are coming from and what gray_sum and m_list_l are. Can you update your question to include a runnable example that produces the error? Commented Dec 16, 2016 at 6:02

2 Answers 2

1

The first element you insert should be a list, not an int. Change m_list_l.append(max(gray_sum[s:e])) to m_list_l.append([max(gray_sum[s:e])]) to fix this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much..Worked fine.I changed the declaration to 'm_list_l= [[x] for x in range(0,4800)]' and it worked.
0

Say there are two lists as

A = [i for i in range(10,100,10)]
A
[10, 20, 30, 40, 50, 60, 70, 80, 90]

B = [i for i in range(20,100,10)]
B
[20, 30, 40, 50, 60, 70, 80, 90, 100]

The combined list would be

L = [[i,j] for i,j in zip(A,B)]
L
 [[10, 20],
 [20, 30],
 [30, 40],
 [40, 50],
 [50, 60],
 [60, 70],
 [70, 80],
 [80, 90],
 [90, 100]]

1 Comment

list(zip(A,B)) will do the same. You do not need list comprehension to achieve this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.