- a.
Explanation of Solution
List method “index()”:
- • “index()” method in Python finds an element in the list and outputs the element’s index.
- • This method is useful in finding the position of the given element in the list.
- • The “index()” method takes an element as an input and searches the list for that element appearance and outputs its position.
- • If more than one occurrence of the element is present in the list, then the method outputs the lowest index.
- • If the element is not present in the list, then this method raises a “ValueError” exception.
Syntax:
In Python, “index()” has the following syntax,
element_index = List_name.index(element)
Explanation:
In the above code,
- • The variable “element_index” stores the return value from the “insert()” method.
- • The variable “List_name” represents the name of the list.
- • The variable “element” indicates the element that is going to be searched in the list.
Example program:
Consider the following example that demonstrates the “index()” method...
- b.
Explanation of Solution
List method “insert()”:
- • The “insert()” method in Python adds an element to the list.
- • This method places an element in the list at a given index.
- • This method takes an element and its position as input parameters and places the given element at the indicated position.
- • When the element is inserted in the list, the list size is increased to hold the given element.
- • The element at the current index and the elements following it are moved by one place to the list end.
- • This method never throws an exception even if the index is not a valid value.
- • If the index value that is greater than the list size is specified as element’s position, then this method inserts the element at the list end.
- • If the index value is negative and it indicates an invalid position, then this method places the element at the list beginning.
Syntax:
In Python, “insert()” has the following syntax,
List_name.insert(element_index, element)
Explanation:
In the above code,
- • The variable “List_name” represents the name of the list.
- • The variable “element_index” indicates the position where the “element” to be inserted in the list.
- • The variable “element” indicates the element that is going to be inserted in the list.
Example program:
Consider the following example that demonstrates the “insert()” method...
- c.
Explanation of Solution
List method “sort()”:
- • “sort()” method sorts the list elements.
- • This method arranges the elements in the list in the ascending order.
- • So that the elements in the list are arranged from lower to higher values.
- • This method does not receive any input and does not return a value.
Syntax:
In Python, “sort()” has the following syntax,
List_name.sort()
Explanation:
In the above code,
- • The variable “List_name” represents the list name that is to be sorted.
Example program:
Consider the following example that demonstrates the “sort()” method.
#Function definition
def main():
#Create a list to store the names
mynames_list = ['John', 'Tom', 'David', 'Kim']
#Print message to the user
;&#x...
- d.
Explanation of Solution
List method “reverse()”:
- • “reverse()” method reverses the list elements.
- • This method changes the elements’ orders in the list.
- • So that the list contains the elements in reverse order.
- • This method does not obtain any input and does not output a value.
Syntax:
In Python, “reverse()” has the following syntax,
List_name.reverse()
Explanation:
In the above code,
- • The variable “List_name” represents the list name that is to be reversed.
Example program:
Consider the following example that demonstrates the “reverse()” method.
#Function definition
def main():
#Create a list to store the numbers
mynumbers_list = [1, 15, 11, 21, 12]
#Print message to the user
print('The numbers list before r...
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Student Value Edition for Starting Out with Python (3rd Edition)
- In Python,Given the following list, list = [9, 3, 0, -4, 8, 7, 10, -1, 5], ask user to give you the following threeindices, start, stop, step. Print list, e.g., li[start:stop:step] based on the indices the user has givenarrow_forwardL = ['jon', 'bran', 'ghost', 'ned', 'sansa', 'arya', 'tyrion', 'drogon'] In the table below, show the contents of the list after each of the first four iterations of the for-loop in selection_sortarrow_forwardplease have the user enter the numbers in the list. Write a function to check that the two vectors (lists) are the same size as well as doing what the problem says. PYTHONarrow_forward
- L = ['arya', 'bran', 'drogon', 'gendry', 'hodor', 'jon', 'sansa', 'tyrion'] Fill out the below table, tracing the call v = binary_search(L, 'daenerys'), a binary search for 'daenerys' in the list L. Fill out one row per iteration of the loop. If there are more rows than iterations, leave extra rows blank. At the end, write the function’s return value v. if element is not found, return None.arrow_forwardWrite statements to do the following: i. Write a loop that computes the sum of all elements in the array, A ii. Write a loop that finds the minimum element in the array, A iii. Write a method to find the maximum in the array, Aarrow_forwardAn array index should normally be of type float. State whether the statement is true or false. If false, explain why.arrow_forward
- Modify the following program to read dictionary items from a file and write the inverted dictionary to a file: characters = {'Bugs':['Rabbit', 1940, 'boy'], 'Garfield': ['Cat', 1978, 'boy'], 'Tweety': ['Bird', 1942, 'girl'], 'Sandy': ['Squirrel', 1999, 'female']} print("Printing the original dictionary") print(characters) print("") def invert_dict(characters): inverse = dict() for key in characters: val = characters[key] for list_item in val: if list_item not in inverse: inverse[list_item] = [key] else: inverse[list_item].append(key) return inverse my_invert = invert_dict(characters) print("Printing the inverted dictionary") print(my_invert) Include the following: 1) The input file for your original dictionary (with at least six items).2) The Python program to read from a file, invert the dictionary, and write to a different file.3) The output file for your inverted dictionary.…arrow_forward1. Practice Conditional Expressions: python Write a function, record_result, that returns “Record Broken!” if the first parameter is less than the second parameter and “Record remains unchanged” otherwise. Do this in one line using a conditional expression. print(record_result(2.31,2.32)) 2. Practice Sets: python Write a function, sort_unique, that returns a list of cities sorted alphabetically after duplicates have been removed. Do this in one line using casting. print(sort_unique(["NYC","ATL","ATL"]))arrow_forwardExchange sort these numbers. And include steps please 7 8 9 3 2 6 4 1 5 0arrow_forward
- Create a function that takes each value of the averaged sequence and passes itthrough the mathematical function if x (the averaged value) is not equal to 5 andreturns 0 if x is equal to 5. These values are returned into a new list. You are only allowed to use the following functions/methods: print(),range(), len(), sum(), max(), min(), and .append().arrow_forwardProgram to Generate Random Numbers from 5 to 30 and Append Them to the Listarrow_forwardDifferentiate between VIEW and COPY? This question is based on arrays in Python.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education