0

I am quite new to the python coding scene and is currently working on a code on Python using a ultrasonic sensor, I want to add the output value into a list (maintaining the list size)where the list is constantly updating with the latest value from the ultrasonic sensor basically OVERWRITTING the list in a sense,

I have seen examples of Append but they are from fix values,

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]

is there anyway to append with the output from the ultrasonic sensor? Thank you very much

my_list=[100,50,10,20,30,50] #current list

#example of expected output
distance=300
distance=250
distance=230

my_list=[230,250,300,100,50,10]  
# the latest 3 reading will be appended to the front of the list 
# while still maintaining the size of my_list

Do let let me know if further clarification or information is needed, thank you in advance.

Manvir
  • 769
  • 1
  • 7
  • 15
yong jie
  • 85
  • 10
  • Does this answer your question? [What is the difference between Python's list methods append and extend?](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) – Manvir Nov 03 '20 at 04:32
  • 1
    hi, I updated my question for a clearer explanation, the page has given me a deeper explanation for append and extend and the differences but I do not think it solves my question, thank you for suggestion – yong jie Nov 03 '20 at 05:20
  • 1
    Do you always want to maintain the same size for the list and append to the front? Because in the example it seems like you append to the front and maintain the same size for the list. – Manvir Nov 03 '20 at 05:32
  • 1
    Yes, That's exactly it, the same size list and just append it to the front basically overwriting the list in a way, sorry if it was unclear before – yong jie Nov 03 '20 at 05:34

4 Answers4

2
my_list.append(distance)

.append() is a built in function in lists.

Student
  • 23
  • 5
1

you can do this:

distance = 300
distance = 250
distance = 230
my_list = [230, 250, 300, 100, 50, 10]
my_list.append(20)
print(my_list)

this will append 20 to the list and the result is [230, 250, 300, 100, 50, 10, 20]

and if you like to append it to a certain position of your list, then use insert

distance = 300
distance = 250
distance = 230
my_list = [230, 250, 300, 100, 50, 10]
my_list.insert(0, 20) # 0 is the position and 20 is what to append
print(my_list)

at this code, 0 is the position and 20 is what to append. you can change the value

After reading your question carefully this is the code:

my_list=[100,50,10,20,30,50] #current list
threelist = []
for _ in range(4):
    distance=300 # your senser value
    threelist.append(distance)
while len(threelist) > 3: 
    del threelist[0]
my_list = threelist + my_list
while len(my_list) > 6:
    del my_list[-1]
print(my_list)

this code appends distance to threelist then + it to the my_list. and del the last 3 value.
and you can change distance=300 to your senser value. I hope this helped you.

ppwater
  • 2,315
  • 4
  • 15
  • 29
  • 1
    ah yes, but the currently the distance is the value coming in from the ultrasonic sensor and it is constantly updating, so appending a fix value is not my goal but very useful to know,thank you. – yong jie Nov 03 '20 at 05:03
  • 1
    @yongjie I don't know it's the right answer for you but I have updated my answer. – ppwater Nov 03 '20 at 05:20
  • 1
    Ah i see, yes you are right that the value is constantly changing, but does not exactly solve my problem but does help me understand how the append works, I have also updated my question, maybe that will provide a clearer explanation? Regardless, I appreciate your help, Tysm – yong jie Nov 03 '20 at 05:27
  • 1
    @yongjie I edited my answer. and thanks for giving update for your question – ppwater Nov 03 '20 at 06:10
1

You can append to the front with the insert function

my_list.insert(0, distance)

and delete last element with

del my_list[-1]

Here I am assuming you are getting sensor data within a loop one at a time.

Manvir
  • 769
  • 1
  • 7
  • 15
1

For every value, you can use my_list.insert(0, new_value) which will insert new value to 1st position. Your code will look something like:

    my_list.insert(0, new_distance) # add new value to beginning (0 index)
    my_list = my_list[:-1] # remove last element to keep the size same

You can also combine those, eg: my_list[:-1].insert(0, new_distance)

SajanGohil
  • 960
  • 13
  • 26