-1

I have a list, say, a=[0,1,2,3]. How can I print an output that moves 0 through all the elements one by one and prints them?

I have tried the list.pop() and list.rotate(), but they just do it once and I cannot seem to loop it over the entire list.

Desired output

[0,1,2,3]
[1,0,2,3]
[1,2,0,3]
[1,2,3,0]
Mazdak
  • 105,000
  • 18
  • 159
  • 188
Freddie
  • 944
  • 9
  • 16
  • Why don't you use Numpy? – Mazdak Jun 16 '19 at 19:50
  • how can I do that? – Freddie Jun 16 '19 at 19:53
  • You wanna replace the main diagonal of a matrix with zeros! So, create a matrix or a 2d array in numpy and learn about the arrays the their structure then how to do the rest :). Hint: https://docs.scipy.org/doc/numpy/reference/generated/numpy.diag_indices.html – Mazdak Jun 16 '19 at 19:58
  • 1
    @Kasrâmvd: The OP does not just want to set the main diagonal to zeros. Note that all columns other than the first and the last have varying values--the second column has both `1` and `2`, for example. So setting up the matrix would be non-trivial. It would be easier to just slice and rearrange the list and print the results. – Rory Daulton Jun 16 '19 at 20:18
  • Possible duplicate of [Generating circular shifts / reduced Latin Squares in Python](https://stackoverflow.com/questions/5313900/generating-circular-shifts-reduced-latin-squares-in-python) – Error - Syntactical Remorse Jun 16 '19 at 21:19

2 Answers2

0
# we don't modify the original list, l
l = [0,1,2,3]
l_copy = l.copy()
l_0 = l_copy.pop(0)
for i in range(len(l_copy) + 1):
    l_copy_2 = l_copy.copy()
    l_copy_2.insert(i, l_0)
    print(l_copy_2)
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • Thank you. but may I ask why we don't modify the original list? when I run this `l = [0,1,2,3] l_0 = l.pop(0) for i in range(len(l) + 1): l_copy_2 = l.copy() l_copy_2.insert(i, l_0) print(l_copy_2)` it works the same – Freddie Jun 16 '19 at 20:19
  • @Faaiz Qadri: It wasn't clear whether it was desirable to modify the original list. he just stated he wanted to print out a certain result. If you want to modify the original list, replace **l_copy_2** in the above code with **l**. Then **l** will take on all the successive values and end up being [1,2,3,0]. Your code is not quite the same thing; you're just clobbering **l** by removing the first element.. – Booboo Jun 16 '19 at 20:25
0

Here is one way. It uses slices to choose various parts of the list then put them back together in a different way. This moves the first element of the list through various positions in the list. Note that this code could be sped up slightly, at the cost of an additional line of code, by calculating and storing a[:1] before the loop, since that does not change between executions of the loop. You should also note that Python's list does not have a rotate method, so I am not sure just what you were trying.

a =[0, 1, 2, 3]

for i in range(len(a)):
    print(a[1:i+1] + a[:1] + a[i+1:])

The printout from that is what you want, though with a space after each comma, as is standard in Python.

[0, 1, 2, 3]
[1, 0, 2, 3]
[1, 2, 0, 3]
[1, 2, 3, 0]

If you really want no spaces in the output, you could use

a =[0, 1, 2, 3]

for i in range(len(a)):
    print(str(a[1:i+1] + a[:1] + a[i+1:]).replace(' ', ''))

which gives the output

[0,1,2,3]
[1,0,2,3]
[1,2,0,3]
[1,2,3,0]
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • Thanks Rory. I was referring to this [link](https://stackoverflow.com/questions/9457832/python-list-rotation?noredirect=1&lq=1) – Freddie Jun 16 '19 at 20:36
  • @FaaizQadri: I see. If you look closely at that link, you see that the `rotate` method is what the questioner wanted, but what he got instead was a `rotate` function. There is no `rotate` method for lists. – Rory Daulton Jun 16 '19 at 20:39
  • I did, but I do not have enough reputations so far. – Freddie Jun 16 '19 at 20:42