0% found this document useful (0 votes)
432 views4 pages

List Exercises in Python

The document describes 3 functions: 1. unpack_and_reverse returns a tuple with the first 3 items of the input tuple in reverse order. 2. string_length returns a 2-tuple containing the input string and its length. 3. modify_list takes a list, sorts it, reverses it, removes the last 3 items, removes one 7 if present, and doubles the first and third items, returning the result.

Uploaded by

hp homeprinter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
432 views4 pages

List Exercises in Python

The document describes 3 functions: 1. unpack_and_reverse returns a tuple with the first 3 items of the input tuple in reverse order. 2. string_length returns a 2-tuple containing the input string and its length. 3. modify_list takes a list, sorts it, reverses it, removes the last 3 items, removes one 7 if present, and doubles the first and third items, returning the result.

Uploaded by

hp homeprinter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#Write a function called unpack_and_reverse that will

#accept one parameter, a tuple with at least three items.

#The function should return a new tuple with only the first

#three items, but listed in reverse order.


#Write a function called string_length. string_length should

#have one parameter, a string. It should return a 2-tuple:

#the first item in the 2-tuple should be the string itself,

#and the second item should be the length of the string as

#given by the len() function.


#Write a function called modify_list. modify_list will

#take one parameter, a list. It should then modify the

#list in the following ways, in this order:

# - Sort the list (using the default sort method).

# - Reverse the order of the list.

# - Delete the last three items of the list.

# - Removes one instance the integer 7 from the list, if

# it's present.

# - Double the values of the first and third items in

# the list.

#It should then return the resulting list. You may assume

#the list will start with at least six items.

#Hint: Remember Python is 0-indexed. The second item

#does not have an index of 2.

#Hint 2: Remember, the list.remove() function removes items

#by value, not by index. Note also that if the item you're

#trying to remove is not found in the list, remove() will

#throw an error: so, you'll want to avoid that one way or

#another!

You might also like