How do I reverse a sublist in a list in place?

前端 未结 12 2669
-上瘾入骨i
-上瘾入骨i 2021-02-20 01:24

I\'m supposed to create a function, which input is a list and two numbers, the function reverses the sublist which its place is indicated by the two numbers. for example this is

12条回答
  •  难免孤独
    2021-02-20 02:27

    Not sure if you have a similar problem as mine, but i needed to reverse a list in place.

    The only piece I was missing was [:]

    exStr = "String"
    
    def change(var):
      var[:] = var[::-1] # This line here
    
    print(exStr) #"String"
    change(exStr)
    print(exStr) #"gnirtS"
    

提交回复
热议问题