-1

I have searched and found how to get the insert to the last on list but I am now curious as to why [-1] does not work.

Travis Harmon
  • 31
  • 1
  • 2
  • Why would you expect that to work? If you tell us why you expected it to work, we can explain what you've misunderstood. – user2357112 Dec 04 '16 at 01:47
  • Sorry just realized I used brackets and im very new to asking question and everything. Ok so i have games = [] games.append ('skyrim') games.append ('gta5') games.insert (-1, american truck simulator) But this adds american truck simulator to the second from last in the list. I did figure out how to add to the last but now I want to know why the games.insert (-1, american truck simulator) doesnt work. – Travis Harmon Dec 04 '16 at 01:50
  • `list.insert` takes two arguments - the index at which you want to insert an object, and the object to be inserted. Also, the use of square brackets isn't right. Try `my_list.insert(3, 'apples')`. – Andrew Guy Dec 04 '16 at 01:52
  • Also having trouble formatting the text here as well it seems – Travis Harmon Dec 04 '16 at 01:55

3 Answers3

5

Say you have

some_list = [1, 2, 3]

and you do

some_list.insert(-1, 4)

You might think, "okay, -1 means the last index, so it'll insert 4 at the last index". But the last index is index 2, over here:

[1, 2, 3]
#      ^

It'll insert 4 at that index, producing

[1, 2, 4, 3]
#      ^ 4 inserted here

You don't want 4 to end up at the current last index. You want to insert 4 at index 3, an index that isn't even in the list yet. You could do that with

some_list.insert(3, 4)

but it's clearer to do

some_list.append(4)
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • AHHH, I now see what it was trying to do!! Thanks so much. You see I had figured out how to get it there but I wanted to learn WHY what i just learned about -1 wasnt going to work in this case. Again thank you very much! – Travis Harmon Dec 04 '16 at 02:01
2

list.append(element) is what you are looking for list[-1] is notation for accessing the last element

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    `list[-1]` is just regular indexing. No slices involved. – user2357112 Dec 04 '16 at 01:48
  • Yes list.append(element) works just fine but say I have a large list that I want to add to the end of. Im just curious as to why list.insert(-1, element) wont add to end of that list. – Travis Harmon Dec 04 '16 at 01:58
  • Because adding to position `-1` is adding at the `len(list) - 1` position, or one before the end. Click the first link to see the equivalent `insert` usage from the documentation – OneCricketeer Dec 04 '16 at 02:02
-1
# Python Program for array rotation


def array_rotation(array, r, d):
    for item in range(r):
        element = array.pop(-d)
        if d:     # -------------------------------->does left to right rotation
            array.insert(d-1, element)
        else:     # -------------------------------->does right to left rotation
            array.append(element)
    return array


arr = ['10', '20', '30', '40', '50']

rotations = input('Enter number of rotations to make in array: ')
direction = input('Enter the direction for rotation:\n1 for right_to_left rotation\n2 for left_to_right rotation\n')
print(arr)
print(array_rotation(arr, int(rotations), 0 if direction == '1' else 1))