Reputation: 47
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
list3=list2.extend(list1.reverse())
return list3
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
Upvotes: 1
Views: 5866
Reputation: 1
def combine_lists(list1, list2):
Jamies_list.reverse()
final_list = Drews_list + Jamies_list
return final_list
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
Upvotes: 0
Reputation: 13
I used the negative index to append the last element of list1 (i.e. Jamies_list) to list2 (i.e. Drews_list).
def combine_lists(list1, list2):
for n in range(1, len(list1)+1):
list2.append(list1[-n])
return list2
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
Upvotes: 0
Reputation: 1
Try this.
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
list1.reverse()
return(list2 + list1)
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Drews_list, Jamies_list))
Upvotes: 0
Reputation: 340
def combine_lists(list1, list2):
list1.reverse()
for list in list1:
list2.append(list)
return list2
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
Upvotes: 0
Reputation: 131
As others have already explained, reverse() and extend() methods are in-place and return None.
If you still want to use extend() and reverse() methods, then you can do it like this:
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
list3 = list1[:]
list4 = list2[:]
list3.reverse()
list4.extend(list3)
return list4
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
If you don't mind changing list1 and list2 in the function, then you can skip copying list1 and list2 into list3 and list4 and directly do:
list1.reverse()
list2.extend(list1)
return list2
Upvotes: 0
Reputation: 586
See if this is what you are looking for :
>>> Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
>>> Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
>>>
>>> Drews_list + Jamies_list[::-1]
['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']
Upvotes: 3
Reputation: 20669
Both .reverse
and .extend
return None
and mutate list in-palce.
Try this.
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
return list1+list2[::-1]
And for reversing you try any of these reversed(list2)
or [::-1]
these methods do not mutate list in-place. Note : reversed(list2)
gives an iterator
Upvotes: 0
Reputation: 1039
def combine_lists(list1, list2):
# Generate a new list containing the elements of list2
# Followed by the elements of list1 in reverse order
buf = list2[:] # or list2.copy()
buf.extend(reversed(list1))
return buf
Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
print(combine_lists(Jamies_list, Drews_list))
extend
method for list modify current list but does not return anything, so your expression: list3=list2.extend(list1.reverse())
assaign to list3
value None
Same about reverse
. It modifies current list, but return None
. You can replace it with reverse(list)
method.
Upvotes: 0