1

I performed measurements with two independent sensors. Due to some whatever reason, one of the sensors became bad during testing, and I would like to make a new list containing the five first elements of sensor1 and the remaining elements from sensor2. I managed to do this simply by:

updated = []            
index   = 0
while index < len(sensor1):
    if index <= 2:
        updated.append(sensor1[index])
    elif index > 2:
        updated.append(sensor2[index])
    else:
        print('You did something wrong, you ignorant fool!')
    index += 1

However, in order to get more accustomed to Python, I would like to translate this to a function named Updater

def Updater(one, two, divide):
    updated = []
    index   = 0
    while index < len(one):
        if index <= divide:
            updated.append(one[index])
        elif index > divide:
            updated.append(two[index])
        else:
            print('You did something stupid, you ignorant fool!')
        index += 1
        return updated

which I call by

data = Updater(one=sensor1, two=sensor2, divide=4)

or

data = [Updater(a, b, divide=4) for a, b in zip(sensor1, sensor2)]

Alas, Updater does not work, as it only performs the first iteration, so the index is equal to 1, although it should be equal to 13, which is the length of the sensor1 and sensor2.

  • What am I doing wrong?
  • How can I make this specific piece of code work within a function?
1
  • 4
    Just a cursory glance suggests that your problem might be because return updated is indented once too many times.
    – pault
    Commented May 3, 2019 at 16:35

4 Answers 4

9

The problem is that you return updated on the first iteration.

Moreover, you could just do

result = one[:divide] + two[divide:]
0
3

Using your current script:

sensor_1 = [1,2,3,4,5]

sensor_2 = [6,7,8,9,20,22,23]

def Updater(one, two, divide):
    updated = []
    index = 0
    while index < len(one):
        if index <= divide:
            updated.append(one[index])
        elif index > divide:
            updated.append(two[index])
        else:
            print('You did something stupid, you ignorant fool!')
        index += 1
        return updated

print(Updater(sensor_1, sensor_2, 2))

We get the following output:

[1]

This is because since the return statement is inside of the while loop, once the loop is executed once it will return the first element in sensor_1 and exit the loop immediately. However pushing the return statement back one indentation level:

sensor_1 = [1,2,3,4,5]

sensor_2 = [6,7,8,9,20,22,23]

def Updater(one, two, divide):
    updated = []
    index = 0
    while index < len(one):
        if index <= divide:
            updated.append(one[index])
        elif index > divide:
            updated.append(two[index])
        else:
            print('You did something stupid, you ignorant fool!')
        index += 1
    return updated

print(Updater(sensor_1, sensor_2, 2))

Output:

[1, 2, 3, 9, 20]
0
2

I would use the tools already available in the itertools module:

from itertools import chain, islice

def updater(one, two, divide):
    # Similar to return one[:divide] + two[divide:]
    return list(chain(islice(one, divide), islice(two, divide,  None)))

islice(one, divide) yields the first 5 elements of one; islice(two, divide, None) yields everything from the sixth element onward from two. chain concatenate the two iterators together, and list builds iterates over the result and builds a list from the elements seen.

Slices will create copies of (portions of) the lists before concatenating them. islice simply returns an iterator that will only yield the request elements without making any copies. This will also work for arbitrary iterables, not just lists.

You can also defer the decision to create a concrete list to the caller:

def updater(one, two, divide):
    return chain(islice(one, divide), islice(two, divide, None))

updated = list(updater(one, two, divide))

This is useful if you never actually need a full list in memory of the result, such as if you only plan on iterating over it:

for x in updater(one, two, 5):
    ...
0
1

I think you don't need that if what you want is just to take a sub list you can do this easily like the following, assuming you have 10 elements:

>>> updated.append(sensor1[0:6])
>>> updated.append(sensor2[6:10])
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.