1

This my code:

num= [2,10]

def calculo(lista):
    lista2 = []    
    for i in lista:
        for j in range(1, i + 1):
            if i % j == 0:
                lista2.append(j)
    return lista2

print(calculo(num))

Which produces: [1, 2, 1, 2, 5, 10]. However, I need the following result:

[[1, 2], [1, 2, 5, 10]]

In addition, I would like to know your approach through list comprehension.

1
  • I'm not sure why you got downvoted even if there's a duplicate out there, which I'm too lazy to find, this seems like a fairly reasonable question. Commented Mar 10, 2020 at 12:25

5 Answers 5

2

This will foreach i loop append new list and then append new values to the last list

num= [2,10]

def calculo(lista):
    lista2 = []    
    for i in lista:
        lista2.append([])
        for j in range(1, i + 1):
            if i % j == 0:
                lista2[-1].append(j)
    return lista2

print(calculo(num))

Also in short form:

num= [2,10]

def calculo(lista):
    lista2 = [[j for j in range(1, i+1) if i % j == 0] for i in lista]
    return lista2

print(calculo(num))
Sign up to request clarification or add additional context in comments.

Comments

2

You will need to create a new list for every iteration of the loop:

outer_list = []
for i in lista:
    inner_list = []
    for j in range(...):
        if ...:
            inner_list.append(...)
    outer_list.append(inner_list)
return outer_list

Comments

2

if you want to use list comprehension

[[x for x in range(1, n+1) if n%x==0] for n in num]

6 Comments

This is not the same as OP's loop.
Did you actually run it?
Yes I did. Enumerate wasn't necessary but besides that it should be fine or not?
Should be fine. Enumerate was the problem. Please fix
I edited the answer, however it also worked before :)
|
1

You need to make the nested lists before the nested loop, just like you make the outer list before the outer loop:

num = [2,10]

def calculo(lista):
    lista2 = []    
    for i in lista:
        lista3 = []
        for j in range(1, i + 1):
            if i % j == 0:
                lista3.append(j)
        if lista3:
            lista2.append(lista3)
    return lista2

print(calculo(num))

Any time you have a sufficiently simple loop that appends items to a list, you can turn it into a list comprehension. For example, the inner loop becomes:

lista3 = [j for j in range(1, i + 1) if i % j == 0]

Now you can write the entire function as:

def calculo(lista):
    return [[j for j in range(1, i + 1) if i % j == 0] for i in lista]

Comments

1
num = [2,10]

def calculo(lista):
    result = [[j for j in range(1, i + 1) if i % j == 0] for i in lista]
    return result

print(calculo(num))

Output:

[[1, 2], [1, 2, 5, 10]]

Comments

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.