Skip to content

Commit 8137b00

Browse files
committed
Merge branch 'list_remove' of git://github.com/chipaca/micropython into chipaca-list_remove
2 parents aa35fc6 + d52a031 commit 8137b00

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

py/objlist.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,23 @@ static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
226226
return mp_const_none;
227227
}
228228

229+
static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
230+
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
231+
mp_obj_t args[] = {self_in, value};
232+
args[1] = list_index(2, args);
233+
list_pop(2, args);
234+
235+
return mp_const_none;
236+
}
237+
229238
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
230239
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
231240
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
232241
static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
233242
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
234243
static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
235244
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
245+
static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
236246
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
237247

238248
const mp_obj_type_t list_type = {
@@ -251,6 +261,7 @@ const mp_obj_type_t list_type = {
251261
{ "index", &list_index_obj },
252262
{ "insert", &list_insert_obj },
253263
{ "pop", &list_pop_obj },
264+
{ "remove", &list_remove_obj },
254265
{ "sort", &list_sort_obj },
255266
{ NULL, NULL }, // end-of-list sentinel
256267
},

tests/basics/tests/list_remove.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a = [1, 2, 3]
2+
print(a.remove(2))
3+
print(a)
4+
try:
5+
a.remove(2)
6+
except ValueError:
7+
print("Raised ValueError")
8+
else:
9+
raise AssertionError("Did not raise ValueError")

0 commit comments

Comments
 (0)