You can use the sequence method list.extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.
>>> lst = [1, 2]
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> lst.extend([5, 6, 7])
>>> lst.extend((8, 9, 10))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> lst.extend(range(11, 14))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
So you can use list.append() to append a single value, and list.extend() to append multiple values.
You can use the sequence method list.extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.
>>> lst = [1, 2]
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> lst.extend([5, 6, 7])
>>> lst.extend((8, 9, 10))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> lst.extend(range(11, 14))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
So you can use list.append() to append a single value, and list.extend() to append multiple values.
Other than the append function, if by "multiple values" you mean another list, you can simply concatenate them like so.
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]
Videos
Unless there is no other way.
first = 35.72438966508524
second = 35.73839550991734
third = 35.81944190992304
forth = 35.80549149559467
fifth = 35.78399019604507
sixth = 36.03781192909738
seventh = 35.9957696566448
eighth = 35.94692998938782
now to make a new numpy array:
a = np.array([first, second, third, forth, fifth, sixth, seventh, eighth])
output:
a
Out[89]:
array([35.72438967, 35.73839551, 35.81944191, 35.8054915 , 35.7839902 ,
36.03781193, 35.99576966, 35.94692999])
to append to existing array (using previously created 'a'):
a = np.append(a, [first, second, third, forth, fifth, sixth, seventh, eight], axis=0)
which gives:
a
Out[93]:
array([35.72438967, 35.73839551, 35.81944191, 35.8054915 , 35.7839902 ,
36.03781193, 35.99576966, 35.94692999, 35.72438967, 35.73839551,
35.81944191, 35.8054915 , 35.7839902 , 36.03781193, 35.99576966,
35.94692999])
Correct syntax is (if I suppose that you wanted to append your 8 values to an numpy array named as ar:
np.append(ar, (first_1, first_2, first_3, first_4, first_5, first_6, first_7, first_8))
- The first argument is your original numpy array,
- The second one is the tuple (or the list, or other array-like object) of your values, so those values have to be in the parentheses.
I am getting real-time rainfall data from an API and want to store the data in multiple lists. Each list represents a different location. There can be a varying number of different locations.
Below is what I have done so far. It sits in a while loop and works but seems to be very ineffcient.
Is there a better way?
r_1 = [] r_2 = [] r_3 = [] r_4 = [] r_5 = [] r_1.append(city_loc.rain[0].current_mm) print(r_1) r_2.append(city_loc.rain[1].current_mm) print(r_2) r_3.append(city_loc.rain[2].current_mm) print(r_3) r_4.append(city_loc.rain[3].current_mm) print(r_4) r_5.append(city_loc.rain[4].current_mm) print(r_5)
It doesn't have to use lists. What I ultimately want to do is to check the fluctuations (eg if r_1[-1] > 22 and r_1[-2] < 10) for each location as quickly as possible so if there is a better way please let me know.
Thanks