Write a function print matching_indexes (items, target) that prints the indexes of all the occurrences of the target in the list items. Check the test cases for how the function should work. Note: Your answer must use a for loop. You are not allowed to use a while loop. For example: Test Result nums = [10, 20, 30] print_matching_indexes (nums, 20) 1 pets = ['dog', 'cat', 'fish', 'cat', 'dog', 'iguana'] 1 print_matching_indexes (pets, 'cat') 3 empty = [) print_matching_indexes (empty, 42)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Iterating Over Lists Revisited
Now that we know about the range function, we have another way of iterating over a list. Try running the following example. What does it produce?
cities = ['Auckland', 'Wellington', 'Christchurch', 'Dunedin']
for i in range(len(cities)):
city = cities [i]
print(i, city)
Notes:
• The output of this example is very similar to example (a) in the 'Iterating Over Lists' info box. In fact, you could make it behave identically by
changing the final line to print(city) instead of print(i, city).
In this example, the for loop iterates over the indexes of the elements of the list, rather than the elements of the list themselves.
• It does this by using the range function, which gives us a range of numbers, starting at 0 and going up to (but not including) len(cities). So in the
example, i willI be 0 the first time around the loop, then 1, then 2, and finally 3.
• This means that i can be used to index into the list cities in order to extract the element (city) that corresponds to index i.
• This means we can print both the city and its index (i) in the final line.
This structure is useful when we want to access the indexes of the elements as well as the elements of the list themselves.
Write a function print_matching_indexes (items, target) that prints the indexes of all the occurrences of the target in the list items. Check the test
cases for how the function should work.
Note: Your answer must use a for loop. You are not allowed to use a while loop.
For example:
Test
Result
nums = (10, 20, 30]
1
print_matching_indexes (nums, 20)
pets = ['dog', 'cat', 'fish', 'cat', 'dog', 'iguana']
1
print_matching_indexes (pets, 'cat')
empty = []
print_matching_indexes (empty, 42)
Transcribed Image Text:Iterating Over Lists Revisited Now that we know about the range function, we have another way of iterating over a list. Try running the following example. What does it produce? cities = ['Auckland', 'Wellington', 'Christchurch', 'Dunedin'] for i in range(len(cities)): city = cities [i] print(i, city) Notes: • The output of this example is very similar to example (a) in the 'Iterating Over Lists' info box. In fact, you could make it behave identically by changing the final line to print(city) instead of print(i, city). In this example, the for loop iterates over the indexes of the elements of the list, rather than the elements of the list themselves. • It does this by using the range function, which gives us a range of numbers, starting at 0 and going up to (but not including) len(cities). So in the example, i willI be 0 the first time around the loop, then 1, then 2, and finally 3. • This means that i can be used to index into the list cities in order to extract the element (city) that corresponds to index i. • This means we can print both the city and its index (i) in the final line. This structure is useful when we want to access the indexes of the elements as well as the elements of the list themselves. Write a function print_matching_indexes (items, target) that prints the indexes of all the occurrences of the target in the list items. Check the test cases for how the function should work. Note: Your answer must use a for loop. You are not allowed to use a while loop. For example: Test Result nums = (10, 20, 30] 1 print_matching_indexes (nums, 20) pets = ['dog', 'cat', 'fish', 'cat', 'dog', 'iguana'] 1 print_matching_indexes (pets, 'cat') empty = [] print_matching_indexes (empty, 42)
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education