What are ways we can combine different list slices?

Question

In this code challenge, one possible way to remove an element might be by combining list slices. What are ways that we can combine list slices?

Answer

Since list slices are themselves lists, we can simply combine them as we would lists. The following are some ways you can use to combine different lists.

Say, for example, we have the following list and two list slices.

list = [1, 2, 3, 4, 5, 6]

list1 = list[:3] # [1, 2, 3]
list2 = list[4:] # [5, 6]

One way to combine them is using the + operator, which will create a new list with the first list’s elements followed by the second list’s elements. For example,

list2 + list1 # [5, 6, 1, 2, 3]

Another way to combine lists is using the .extend() method in Python. This method is applied to a list, and adds the elements from the list passed as an argument to the end of that list. This updates the list in-place.

list1.extend(list2)
print(list1) # [1, 2, 3, 5, 6]

One other way of combining lists is to utilize a loop. You can iterate over the elements of the list you are adding to the other list, and use .append() to add them one at a time. Like .extend(), .append() updates the list in-place.

for element in list2:
  list1.append(element)

print(list1) # [1, 2, 3, 5, 6]
11 Likes

Wondering if this code is correct and if so is there a reason we shouldn’t use del to solve this challenge? It seems like it would be a simpler solution than splitting and combining.

def remove_middle(lst,start,end):
  end+=1
  del lst[start:end]
  return lst

print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
1 Like

Much of programming pedagogy seems to consist of asking students to “re-invent the wheel” by coding functionality that is already represented in the list of built-in functions. max() and min() are classic examples. You have certainly hit upon another.

Is it wrong? Should someone be penalized for taking the trouble to research the problem? I suppose that’s up to the instructor. In my opinion, if you understand the “long way,” you are to be commended for discovering something easier, or at least more direct.

2 Likes

Hello friends,

I’m very much a beginner at python and this was my logic flow on how I came about solving the exercise with what has been taught purely through this course. Hope it provides help to others who are starting out just like me :slight_smile:

e.g.
lst =  [4, 8, 15, 16, 23, 42]
print(lst[1])        <<< prints 8
print(lst[3])       <<< prints 16
print(lst[:1])       <<< prints [4]
print(lst[3+1:])   <<< prints [23, 42]

^since we have to remove the ending index too, +1 lets us begin on the next index
^ so, we must remove 8, 15, 16

new_lst=[]

new_lst = new_lst + lst[:1] + lst[3+1:]
print(new_lst)

def remove_middle(lst, start, end):
  new_lst = []
  new_lst = new_lst + lst[:start] + lst[end+1:]
  return new_lst

# streamlined version (you don't need a new list)

def remove_middle(lst, start, end):
  return lst[:start] + lst[end+1:]

print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

#prints [4, 23, 42]
25 Likes

Thank you, @dtsyi! As another beginner whose only frame of reference is what Codecademy has taught me, this logic flow really made it click for me. :+1::+1:

On to the next lesson!

1 Like

Thank you so much @dtsyi your explanation helped a lot.

1 Like

Beautiful in its simplicity.

1 Like

So much clearer with this example.
Thank you

Thank you, this is very helpful

3 posts were split to a new topic: About list slicing

thank you for much for posting this, crystal clear!

Here is my solution. It took me a while but I think its pretty simple and covers only stuff taught in the course (I think)

def remove_middle(lst,start,end):
  end = end+1
  lst1 = lst[:start]
  lst2 = lst[end:]
  lst = lst1[0:] + lst2[0:]
  return lst

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))