Matt
Matt

Reputation: 5095

Python modifying and editing lists

I am just starting to learn python, I was trying out some code and I wanted to ask why the two following codes function differently?

First code sample:

def combine_lists(list1, list2):
  new_list = list2
  index = len(list2)
  for items in list1:
      new_list.insert(index, items)
  return(new_list)
    
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

The output of this first code is correct (where I want list 1 reversed):

['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']

Second code sample:

def combine_lists(list1, list2):
  new_list = list2
  for items in list1:
      new_list.insert(len(list2), items)
  return(new_list)
    
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

But this second code outputs:

['Mike', 'Carol', 'Greg', 'Marcia', 'Alice', 'Cindy', 'Bobby', 'Jan', 'Peter']

Why is there a difference in these two outputs by just assigning the len(list2) to a variable and using that for the index insertion?

Thanks!

Upvotes: 3

Views: 910

Answers (2)

mhhabib
mhhabib

Reputation: 3121

First code snippet


def combine_lists(list1, list2):
  new_list = list2
  index = len(list2)
  for items in list1:
      new_list.insert(index, items)
      print(new_list)
  return(new_list)
    
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

Output:

['Mike', 'Carol', 'Greg', 'Marcia', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Bobby', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Jan', 'Bobby', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']
['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']

For your first code, you're inserting new_list.insert(index, items) every new item on the same position for a certain period ( len(list2) ). Every time when you insert it get the same index and last insert element get move to the index+1 position and that's why it seems Jamies_list the list are inserting reversely.

Second code snippet

Since you're allocating len(list2) every time of for loop, it get dynamically update the len and taking the new the position of the new item

Upvotes: 1

Grismar
Grismar

Reputation: 31319

This line:

    new_list = list2

Just creates a new name for the list2 list, but both variables refer to the same list.

So, in the first example, you get the length of that list with index = len(list2) and then use the same value for index on each iteration through the for loop.

In the second example, you get len(list2) for each iteration, but since you're modifying new_list, you're also modifying list2 and its length will increase as you go.

If you need a copy of the list, create a copy with list(new_list) = list2 or new_list = list2.copy()

Upvotes: 6

Related Questions