2

I have the following code and have two questions.

  1. why does this code result in [[0, 5, 4, 6], [0, 5, 4, 6]] and not [[0, 5], [0, 5, 4, 6]] when using append? I get the expected result if I use extend, but I want a nested list here.

  2. 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)
user1831677
  • 201
  • 1
  • 4
  • 13
  • 1
    I get `[2, 1, 2, 1, 0, 2]` – Christian Tapia Jan 18 '14 at 21:47
  • 1
    If you want more lists, start by creating a new list once in a while instead of each time appending to the same list. – tobias_k Jan 18 '14 at 21:50
  • What you describe does not match your code. What is the problem, and what is the code actually supposed to do? – tobias_k Jan 18 '14 at 21:55
  • The code above will produce a random result each time in the form of [[0, 5, 4, 6], [0, 5, 4, 6]]. It should produce [[0, 5], [0, 5, 4, 6]] and I want to know why and I want to know how I can change the code so the end result is [[0, 5], [4,6]] - my ultimate goal - I can't seem to get there. – user1831677 Jan 18 '14 at 22:08
  • This sounds like http://stackoverflow.com/questions/5280799/list-append-changing-all-elements-to-the-appended-item – dstromberg Jan 18 '14 at 22:09
  • Why exactly has this been put on hold? The code directly addresses question 1 -I asked why the code was producing the result it does. then question 2 I asked how it can be changed to achieve the desired result. What is the issue? – user1831677 Jan 19 '14 at 03:55

3 Answers3

4

You have some errors, try this way:

def RandomNumbers(li, questions):
    if type(li) == list:
        new = []
        for item in li:
            new_list = []
            for i in range(questions):
                new_list.append(randint(item[0], item[1]))
            new.append(new_list)
        return new
    else:
        return ""

Test:

>>> print RandomNumbers([[0, 5], [1, 6]], 2)
[[4, 3], [6, 6]]
>>> print RandomNumbers([[0, 5], [1, 6]], 2)
[[3, 5], [3, 1]]
ndpu
  • 22,225
  • 6
  • 54
  • 69
2

As stated above, your description does not match your code.

But to answer your question why you are getting the same list twice:

  1. You create one object new_list
  2. You append that to new two times. BUT: You are just working with one and the same object all the time, therefore it is added twice and you add all items to this list.

You can work around that by moving the line new_list = [] after the first for-loop.

OBu
  • 4,977
  • 3
  • 29
  • 45
0

You append to the same new_list while iterating over the items of li. You need to create a new list in the loop as suggested by tobias_k.
The reason why you get [[0, 5, 4, 6], [0, 5, 4, 6]] is that you append the reference to new_list to new twice, that means that new[0] and new[1] refer to the same list. Subsequently the first list also contains the elements you append to new_list after appending it to new.

Cu3PO42
  • 1,403
  • 1
  • 11
  • 19
  • Thanks for the explanation. I understand that I was simply appending the same list twice and not a new one as I thought. – user1831677 Jan 19 '14 at 03:48