Question

Python. Write a function swap_lists that takes in two lists and swap their elements in place....

Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41, 65, 43, 4, 17] >>> swap_lists(list1, list2) >>> print(list1) [30, 41, 65, 43, 4, 17] >>> print(list2) [4, 2, 6, 8, 90, 45] """

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your code: -

def swap_lists(alist1, alist2):

"""Swaps content of two lists"""

for i in range(len(alist1)):

alist1[i], alist2[i] = alist2[i], alist1[i]

list1 = [1,2,3,4]

list2 = [5,6,7,8]

print("list before swapping : ")

print("list1: ",list1)

print("list2: ",list2)

swap_lists(list1,list2)

print("list after swapping : ")

print("list1: ",list1)

print("list2: ",list2)

Output and indentation

Add a comment
Know the answer?
Add Answer to:
Python. Write a function swap_lists that takes in two lists and swap their elements in place....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone fix this python program? I'm trying to do three things: Create a function that...

    Can someone fix this python program? I'm trying to do three things: Create a function that takes in a string as a parameter. Then create a dictionary of characters to integers, where the integer represents how many times the number of times the character appears in the word. Create a function that takes two lists as parameters and returns the elements they have in common of the two lists. Ask the user to create a list of numbers and keep...

  • [2.5pts] Write the function connect(listl, list2, k) that takes two lists, listl and list2, and a...

    [2.5pts] Write the function connect(listl, list2, k) that takes two lists, listl and list2, and a non- negative integer k that is less than or equal to the length of listl. It returns (not prints) a new list containing the first k elements of list1, then all elements of list2, then the remaining elements of list1. Hint: slicing could be helpful here

  • TITle Addition of two lists ; Proj1.asm ; This program add elements of two lists separately, disp...

    TITle Addition of two lists ; Proj1.asm ; This program add elements of two lists separately, display their sums, then exchange two sums, and display then againe INCLUDE Irvine32.inc data list1 WORD 1eh, 20h, 30h list2 NORD 10h, 20h, 38h, 48h suml WoRD? sun2 WORD? code main PROc mov ax, [list1] add ax, [list1+2] add ax, [list1+4] mov sumi, ax call DumpRegs nov bx, [list2] add bx, [list2+2 add bx, [list2+4) add bx, [list2.6] nov sum2, bx call DumpRegs xchg...

  • IN python # Given these test lists to work with: salesDollars = [ 20, 30, 35,...

    IN python # Given these test lists to work with: salesDollars = [ 20, 30, 35, 15, 60, 100] personnelCosts = [ 15, 25, 30, 10, 55, 95] salesPersons =[ "jo", "mo", "ko", “curley”, “jack”,”gunnar”] A. Write a generic function that will take two numeric list parameters, of equal length, calculate the total of list1 – total of list 2 and return a triple of numbers, (totalOfList1, totalOfList2, difference of list1 and list2 totals. B. Test your function with salesDollars...

  • 1. Define a function in python that returns the sum of the following 4 lists. Remember...

    1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values. Copy the following lists and paste them in a Python file after you define the sum_of_values function Call the function and pass to it a different list each call as...

  • Write a method called alternate that accepts two Lists as its parameters and returns a new...

    Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order: • First element from first list • First element from second list • Second element from first list • Second element from second list • Third element from first list • Third element from second list If the lists do not contain the same number of elements, the remaining elements from the...

  • Questions Write a method that returns the union of two array lists of integers using the...

    Questions Write a method that returns the union of two array lists of integers using the following header: public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) Write a test program that: 1. prompts the user to enter two lists, each with five integers, 2. displays their union numbers separated by exactly one space., and 3. finds the maximum number and the minimum number in the combined list. Here is a sample run of the program. Enter five integers for list1: 5...

  • pick two answers 12 points). The function swap() should swap two integers and main() should print...

    pick two answers 12 points). The function swap() should swap two integers and main() should print those two swapped integers. What (if anything is wrong with this code? Select all that apply. oooo. No ure W..Ni def main(): X = 10 y = 2 x, y = swap(x,y). print(x,y) def swap(a,b): a = b temp = a b = temp main N on line 9, it is illegal syntax to introduce a new variable named temp. Instead, temp should be...

  • This is python coding recursive function question. Could anyone help me figuring out these not using...

    This is python coding recursive function question. Could anyone help me figuring out these not using built-in function? Thanks. Write a recursive function factorial(n) that returns the value of n!=1⋅2⋅3⋅⋯⋅n. 2. Write a recursive function reverse(text) that returns the reverse of the string named text. 3. Write a recursive function countUpper(s) that returns the number of uppercase letters in the string s. 4. Write a recursive function equal(list1, list2) that returns a Boolean value indicating whether the two lists are...

  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT