Question

Python Write a function list_copy(l) that takes a list as a parameter and returns a copy...

Python Write a function list_copy(l) that takes a list as a parameter and returns a copy of the list using a list comprehension.

please provide unittest

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def list_copy(lst):
    return [x for x in lst]


lst1 = [3, 4, 1, 9]
lst2 = list_copy(lst1)
print(lst1)
print(lst2)
# now proving that they are not same object and they are different objects
lst1.append(2)
print(lst1)
print(lst2)

Add a comment
Know the answer?
Add Answer to:
Python Write a function list_copy(l) that takes a list as a parameter and returns a copy...
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
  • Using Python, write a function named add_surname that takes as a parameter a list of first...

    Using Python, write a function named add_surname that takes as a parameter a list of first names. **It should use a list comprehension** to return a list that contains only those names that start with a "K", but with the surname "Kardashian" added to each one, with a space between the first and last names. For example, if the original list is: ``` ["Kiki", "Krystal", "Pavel", "Annie", "Koala"] ``` Then the list that is returned should be: ``` ['Kiki Kardashian',...

  • Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns...

    Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...

  • using python, Write a function named displayReverse that takes a list as a parameter and DISPLAYS...

    using python, Write a function named displayReverse that takes a list as a parameter and DISPLAYS the contents of the list to the screen in reverse. Submit the code and a screenshot of the program running in Linux

  • Write a Scheme function DEL-LELEMENT that takes a list as a parameter and returns a list...

    Write a Scheme function DEL-LELEMENT that takes a list as a parameter and returns a list identical to the parameter except the last element has been deleted. For example, (DEL-LELEMENT '(8 2 3 7 6)) should return (8 2 3 7) *Please explain your code and submit the source code. Please test the function with the provided example and submit a screen shot of the testing result.

  • Python 2.7 Write a function cumsum() that takes a list l as argument and returns the...

    Python 2.7 Write a function cumsum() that takes a list l as argument and returns the cumulative sum (also known as the prefix sum) of l, which is a list, say cs of the same length as l such that each element cs[i] is equal to the sum of the first i + 1 elements of l, i.e., cs[i] == l[0] + l[1] + l[2] + ... + l[i] You should not modify the argument list l in any way....

  • Python: Write a function named "sort_by_length" that takes a list/array of strings as a parameter and...

    Python: Write a function named "sort_by_length" that takes a list/array of strings as a parameter and sorts the strings by their length

  • Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns...

    Using Python Programming Language: 3. Write a function flatten that takes a 2D list and returns all the items of each list concatenated together into one new 1D list. For example: flatten ([["a", "b"],["c","0"],["e","f"]]) would return ["a", "b","C","d", "e","f"] Save the function in a PyDev library module named functions.py Write a program t03.py that tests flatten function and prints the returned flat list to the screen. Test your program with a different list, hardcoded in t03.py • Copy the results...

  • ** In Python ** Write a function named "tweets" that takes a string as a parameter...

    ** In Python ** Write a function named "tweets" that takes a string as a parameter and returns the number of tweets required to tweet the input to the world. Note: The maximum length for a single tweet is 280 characters Please provided an explanation.

  • Write a function that takes a string as a parameter and returns a new string that...

    Write a function that takes a string as a parameter and returns a new string that is the reverse of the old string. Python from test import testEqual def reverse(s): return s testEqual(reverse("hello"),"olleh") testEqual(reverse("l"),"l") testEqual(reverse("follow"),"wollof") testEqual(reverse(""),"")

  • Write a python function alt which takes a list of strings myList as a parameter. alt...

    Write a python function alt which takes a list of strings myList as a parameter. alt then returns two strings s1 and s2 as a tuple (s1, s2). Here s1 is the concatenation of the strings in myList in even index positions, and s2 is the concatenation of the strings in myList in odd index positions. (List starts at index 0) For example, if myList = [‘My’, ‘kingdom’, ‘for’, ‘a’ , ‘horse’], then s1 = ‘Myforhorse’ and s2 = ‘kingdoma’.

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