Python: Create a new list dividing two given lists of numbers
Divide Elements of Two Lists
Write a Python program to create a new list by dividing two given lists of numbers.
Visual Presentation:
Sample Solution:
Python Code:
# Define a function called 'dividing_two_lists' that divides each element of two lists 'l1' and 'l2' element-wise.
def dividing_two_lists(l1, l2):
# Use 'zip' to pair elements from 'l1' and 'l2', then use a list comprehension to divide each pair (x, y) and create a new list 'result'.
result = [x / y for x, y in zip(l1, l2)]
return result
# Create two lists of numbers 'nums1' and 'nums2'.
nums1 = [7, 2, 3, 4, 9, 2, 3]
nums2 = [9, 8, 2, 3, 3, 1, 2]
# Print a message indicating the original lists.
print("Original lists:")
print(nums1)
print(nums2)
# Call the 'dividing_two_lists' function to divide the elements of 'nums1' by the corresponding elements of 'nums2' and print the result.
print(dividing_two_lists(nums1, nums2))
Sample Output:
Original list: [7, 2, 3, 4, 9, 2, 3] [7, 2, 3, 4, 9, 2, 3] [0.7777777777777778, 0.25, 1.5, 1.3333333333333333, 3.0, 2.0, 1.5]
Flowchart:
Python Code Editor:
Previous: Write a Python program to find the minimum, maximum value for each tuple position in a given list of tuples.
Next: Write a Python program to find common elements in a given list of lists.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics