w3resource

Python: Interleave two given list into another list randomly

Python List: Exercise - 147 with Solution

Write a Python program to combine two lists into another list randomly.

Sample Solution:

Python Code:

# Import the 'random' module to generate random values
import random

# Define a function 'randomly_interleave' that interleaves two lists randomly
def randomly_interleave(nums1, nums2):
    # Create a new list by sampling elements from 'nums1' and 'nums2'
    # The elements from 'nums1' and 'nums2' are interleaved randomly
    result = [x.pop(0) for x in random.sample([nums1] * len(nums1) + [nums2] * len(nums2), len(nums1) + len(nums2))]
    return result

# Create two lists 'nums1' and 'nums2'
nums1 = [1, 2, 7, 8, 3, 7]
nums2 = [4, 3, 8, 9, 4, 3, 8, 9]

# Print a message indicating the original lists
print("Original lists:")
# Print the contents of 'nums1' and 'nums2'
print(nums1)
print(nums2)

# Print a message indicating the operation to interleave the two lists
print("\nInterleave two given lists into another list randomly:")
# Call the 'randomly_interleave' function with 'nums1' and 'nums2', then print the result
print(randomly_interleave(nums1, nums2))

Sample Output:

Original lists:
[1, 2, 7, 8, 3, 7]
[4, 3, 8, 9, 4, 3, 8, 9]

Interleave two given list into another list randomly:
[4, 1, 2, 3, 8, 9, 4, 3, 7, 8, 9, 8, 3, 7]

Flowchart:

Flowchart: Interleave two given list into another list randomly.

Python Code Editor:

Previous: Write a Python program to compute the sum of digits of each number of a given list.
Next: Write a Python program to remove specific words from a given list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/list/python-data-type-list-exercise-147.php