cut_list
We have to use a cut_list by writing a function called cutlist that "cuts" a list. Whwew a list and an index is given, and returns a copy of the list, but the items before and after the index is swapped, but what i dont get is it should run with strings too!

>>> cut_list([0,1,2,3,4,5,6,7,8,9], 3)
[4, 5, 6, 7, 8, 9, 3, 0, 1, 2]
>>> cut_list("ABCDEFGX1234",7) 
'1234XABCDEFG'

this is the code i tried:

listA[4,5,6,7,8,9]
listB[3,0,1,2]
listA.insert(listB)

I am not sure what went wrong??

Use list slicing and extend().
Hint:

mylist = [0,1,2,3,4,5,6,7,8,9]
ix = 3

t1 = mylist[0:ix]
t2 = mylist[ix+1:]

# now use list extend()
t2.extend(t1)

print(t2)  # [4, 5, 6, 7, 8, 9, 0, 1, 2]

Much more cumbersome, but an option is to use reverse, pop then extend ...

mylist = [0,1,2,3,4,5,6,7,8,9]
ix = 3

t1 = []
revlist = list(reversed(mylist))
for k in range(ix+1):
    t1.append(revlist.pop())
# pop element in position ix and discard
t1.pop()

# reverse again then extend
revlist.reverse()
revlist.extend(t1)

print(revlist)  # [4, 5, 6, 7, 8, 9, 0, 1, 2]

Looks like slicing is the way to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.