1

Im trying to keep a log of past list combinations used with the python standard function append/extend etc:

log = [[(3, 23), (5, 7), (6, 14), (3, 0), (11, 24), (3, 5), (20, 19), (16, 9), (19, 5)]]
counter = 0
switch = False

while switch == False:
    counter += 1

    if counter > 5:
        switch = True

    lenth = len(log)
    netlist = log[0]

    index = netlist.index((11, 24))

    tmp = netlist[index - 1]
    netlist[index - 1] = netlist[index]
    netlist[index] = tmp
    print(netlist)

    log.append(netlist)


print()
for lists in log:
    print(lists)

The result should be a log variable containing all the used lists after the switch. So staring with de list in the log and after each switch it should append the list to the log when the loop ends all the used list should me printed.

The result I get is a log with all the same lists (the last one used). First list is how it should me last is how it is now.

First list is how it should me last is how it is now.

Yasser Hussain
  • 854
  • 7
  • 21
Theetje
  • 29
  • 10

1 Answers1

1

There are a couple of misunderstandings which I will attempt to clear.

Misunderstanding 1 - List are immutable

The result I get is a log with all the same lists (the last one used).

Correct understanding - Lists are mutable

When you interchange the values in the list using following code -

tmp = netlist[index - 1]
netlist[index - 1] = netlist[index]
netlist[index] = tmp

You are not creating a new list, you are just modifying the same list.

Read about Lists and mutability here.

The problem with this is as follows -

Your log list will end up containing multiple references to same list object.

So the situation will be something like this.

         __________________________________________________
        |                  netlist                         |
        |                                                  |
        |                  value -                         |
        |  [(3, 23), (5, 7), (6, 14), (3, 0),              |
        |   (11, 24), (3, 5), (20, 19), (16, 9), (19, 5)]  |
        |                                                  |
        |                                                  |
        |                                                  |
        |_____^________________^______________^____________|
        ______|________________|______________|_________________
       |      |                |              |                 |
       |   ___|_____       ____|_____     ____|_____            |
       |  |         |     |          |   |          |           |
       |  |   0     |     |    1     |   |    2     |           |
       |  |_________|     |__________|   |__________|  ........ |
       |                                                        |
       |                  log                                   |
       |________________________________________________________|

Here as you can see the 0th index refers to the same element as 1st index, 2nd index and so on.

What you probably wanted was a log to contain multiple different lists.

Something like so -

         _____________     ____________     _____________
        |             |   |            |   |             |
        | [(3, 23),   |   |  another   |   | yet another |
        |   (5, 7)...]|   |    list    |   | list        |
        |             |   |            |   |             |
        |             |   |            |   |             |
        |_____^_______|   |____^_______|   |____^________| .......
        ______|________________|________________|_______________
       |      |                |                |               |
       |   ___|_____       ____|_____       ____|_____          |
       |  |         |     |          |     |          |         |
       |  |   0     |     |    1     |     |    2     |         |
       |  |_________|     |__________|     |__________| ....... |
       |                                                        |
       |                  log                                   |
       |________________________________________________________|

So when you try to print log what you get is the same list printed again and again.

The solution to this is to copy the initial list using the : operator.

Read how to copy list here.

Misunderstanding 2 - Using 0 to select last element.

Correct understanding - Use -1 to get last element.

This piece of code -

netlist = log[0]

suggests you are trying to select the last element of the list but using index 0.

To get last element in a list use index -1.

Read about slice notation here.

Final Code -

log = [[(3, 23), (5, 7), (6, 14), (3, 0), (11, 24), (3, 5), (20, 19), (16, 9), (19, 5)]]
counter = 0
switch = False

while switch == False:
    counter += 1

    if counter > 5:
        switch = True

    # lenth = len(log) Commenting this line as it's not needed
    netlist = log[-1][:]

    index = netlist.index((11, 24))

    tmp = netlist[index - 1]
    netlist[index - 1] = netlist[index]
    netlist[index] = tmp
    # print(netlist)

    log.append(netlist)

# Remove 1st element of log if you don't need it.
log = log[1:]
print()
for lists in log:
    print(lists)

This prints the following output which is what you expected.

[(3, 23), (5, 7), (6, 14), (11, 24), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(3, 23), (5, 7), (11, 24), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(3, 23), (11, 24), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(11, 24), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(19, 5), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (11, 24)]
[(19, 5), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (11, 24), (16, 9)]
Yasser Hussain
  • 854
  • 7
  • 21