I have the following code and have two questions.
why does this code result in
[[0, 5, 4, 6], [0, 5, 4, 6]]
and not[[0, 5], [0, 5, 4, 6]]
when usingappend
? I get the expected result if I useextend
, but I want a nested list here.I want the result to be
[[0, 5], [4, 6]]
- a nested list with two elements. That is, one for each question (second parameter). So how can I change the code to accomplish this?
Code below:
def RandomNumbers(li, questions):
new_list = []
new = []
if type(li) == list:
for i in range(1, questions +1):
for item in li:
result = randint(item[0], item[1])
new_list.append(result)
new.append(new_list)
else:
return ""
return new
print RandomNumbers([[0, 5], [1, 6]], 2)