Question

def append_length(my_list): """ Append the length to a list Finish this function which gets the length...

def append_length(my_list):
    """
    Append the length to a list
    Finish this function which gets the length of a list, then append the length (as an integer) to the list.
    For example, if the parameter my_list is [0,1,2,3], after running this function, a 4 will be appended to this list:
    [0, 1, 2, 3, 4]
    This function does not have any explicit return value.

    Micro-credential(s):
    - Applying basic operations/operators (including len(), concatenation, repetition and in operator) to lists
    - Adding an item to the end of a list with append() function
    """
#=======================================
my_small_list = [9,8,7,6]
append_length(my_small_list)
print(my_small_list)  # [9, 8, 7, 6, 4]
append_length(my_2d_list)
print(my_2d_list)  # [[88, 1], [5, 67], [52, 60, 7], 3]
print("--"*10)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code in Python: (refer to screenshot below for indentations)

def append_length(my_list):
"""
Append the length to a list
Finish this function which gets the length of a list, then append the length (as an integer) to the list.
For example, if the parameter my_list is [0,1,2,3], after running this function, a 4 will be appended to this list:
[0, 1, 2, 3, 4]
This function does not have any explicit return value.

Micro-credential(s):
- Applying basic operations/operators (including len(), concatenation, repetition and in operator) to lists
- Adding an item to the end of a list with append() function
"""
length=len(my_list)
my_list.append(length)
#=======================================
my_small_list = [9,8,7,6]
append_length(my_small_list)
print(my_small_list) # [9, 8, 7, 6, 4]
my_2d_list=[[88, 1], [5, 67], [52, 60, 7]]
append_length(my_2d_list)
print(my_2d_list) # [[88, 1], [5, 67], [52, 60, 7], 3]
print("--"*10)

Output:

Add a comment
Know the answer?
Add Answer to:
def append_length(my_list): """ Append the length to a list Finish this function which gets the length...
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
  • def double_odd_values(my_list): """ 04. making changes to some values in a list Finish this function takes...

    def double_odd_values(my_list): """ 04. making changes to some values in a list Finish this function takes a list as parameter, and double all the odd values in place. For example, for the parameter [1, 2, 3], after calling this function, the list will be changed to [2,2,6] and the changes will be made in place. You can assume all the items in the parameter are integers. There is no explicit return value for this function. - Updating the items in...

  • Explain this lisp recursive function that gets the length of a list. ; a recursive function...

    Explain this lisp recursive function that gets the length of a list. ; a recursive function to find the lenght of the list (defun len (list) (if list (1+ (len (cdr list))) 0)) What does the 1+ and the 0 at the end do?

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

    Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list) bounds. if the insert_position is greater than the length of the list, insert the value at the end of the list. if the insert_position is less than or equal...

  • 1. What is the result of calling the append method on a list? 2. What must...

    1. What is the result of calling the append method on a list? 2. What must be defined prior to using a method like append? 3. Explain why two lines caused an IndexError. 4. What is the result of calling the remove method on a list? 5. Give one example of a list method that requires an argument and one that does not. 6. Describe the syntax similarities and differences between using a list method like append and Python built-in...

  • Create a Scheme function for the following: Thank you! 2. combine which has 3 arguments that...

    Create a Scheme function for the following: Thank you! 2. combine which has 3 arguments that are all lists. The result should be equivalent to the concatenation of the first list, the reverse of the second list, followed by the third list. Use the built-in functions append and reverse. For example, (combine '(1 2) (3 4 5) '(6 7)) should return the list '(1 2 5 4 3 6 7).

  • Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...

    Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here

  • In LISP write one function using length that gets the last atom in a list and...

    In LISP write one function using length that gets the last atom in a list and another function that triples all the elements in a list (triple-value '(1 2 3 4 5)) so the result should be (1 8 27 64 125)

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • 1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n,...

    1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n, p): """Return a list of the run lengths in n tosses of a coin whose heads probability is p. Arguments: n--Number of tosses (a positive int), p--The probability of observing heads for any given toss (float between 0 and 1). """ For example, if the simulated sequence of coin tosses is HTTHHTHHHTTHHTTTTH, then the list of run lengths the function returns will be [1,...

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