w3resource

Python: Inject a number in between each pair of adjacent numbers in a list of numbers

Python Programming Puzzles: Exercise-71 with Solution

Given a list of numbers and a number to inject, write a Python program to create a list containing that number in between each pair of adjacent numbers.

Input: [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
Separator: 6
Output:
[12, 6, -7, 6, 3, 6, -89, 6, 14, 6, 88, 6, -78, 6, -1, 6, 2, 6, 7]

Input: [1, 2, 3, 4, 5, 6]
Separator: 9
Output:
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6]

Visual Presentation:

Python: Inject a number in between each pair of adjacent numbers in a list of numbers.

Sample Solution-1:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a list of numbers and a separator as input
def test(nums, sep):
    # Initialize a list 'ans' with double the length of 'nums' minus one, filled with separators
    ans = [sep] * (2 * len(nums) - 1)
    
    # Replace every second element of 'ans' with the corresponding elements from 'nums'
    ans[::2] = nums
    
    return ans  # Return the modified list

# Example 1
nums1 = [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
separator1 = 6
print("List of numbers:", nums1)
print("Separator:", separator1)
print("Inject the separator in between each pair of adjacent numbers of the said list:")
print(test(nums1, separator1))

# Example 2
nums2 = [1, 2, 3, 4, 5, 6]
separator2 = 9
print("\nList of numbers:", nums2)
print("Separator:", separator2)
print("Inject the separator in between each pair of adjacent numbers of the said list:")
print(test(nums2, separator2))

Sample Output:

List of numbers: [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
Separator: 6
Inject the separator in between each pair of adjacent numbers of the said list:
[12, 6, -7, 6, 3, 6, -89, 6, 14, 6, 88, 6, -78, 6, -1, 6, 2, 6, 7]

List of numbers: [1, 2, 3, 4, 5, 6]
Separator: 9
Inject the separator in between each pair of adjacent numbers of the said list:
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6]

Flowchart:

Flowchart: Python - Inject a number in between each pair of adjacent numbers in a list of numbers.

Sample Solution-2:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a list of numbers and a separator as input
def test(nums, sep):
    result = []  # Initialize an empty list to store the result
    
    # Iterate through the indices of 'nums'
    for i in range(len(nums)):
        # Check if the current index is the last one
        if i == len(nums) - 1:
            result.append(nums[i])  # If it is the last index, append the number without the separator
        else:
            result.append(nums[i])  # Append the current number to the result
            result.append(sep)      # Append the separator after the current number
    
    return result  # Return the modified list

# Example 1
nums1 = [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
separator1 = 6
print("List of numbers:", nums1)
print("Separator:", separator1)
print("Inject the separator in between each pair of adjacent numbers of the said list:")
print(test(nums1, separator1))

# Example 2
nums2 = [1, 2, 3, 4, 5, 6]
separator2 = 9
print("\nList of numbers:", nums2)
print("Separator:", separator2)
print("Inject the separator in between each pair of adjacent numbers of the said list:")
print(test(nums2, separator2))

Sample Output:

List of numbers: [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
Separator: 6
Inject the separator in between each pair of adjacent numbers of the said list:
[12, 6, -7, 6, 3, 6, -89, 6, 14, 6, 88, 6, -78, 6, -1, 6, 2, 6, 7]

List of numbers: [1, 2, 3, 4, 5, 6]
Separator: 9
Inject the separator in between each pair of adjacent numbers of the said list:
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6]

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the first negative balance.
Next: Find the indices of three numbers that sum to 0 in a 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://w3resource.com/python-exercises/puzzles/python-programming-puzzles-71.php