Def function on a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Def function on a list

Is there a way to operate with a list using some def function? I need to rng, then remove multiple elements from multiple lists, one by one, so in order to avoid too much typing, def is pretty much required. With as little knowledge as i have, it seems impossible.

22nd Jun 2018, 3:03 PM
Igor Matić
Igor Matić - avatar
6 Answers
+ 3
Like this? from random import choice def morflst(list): a=choice(list) list.remove(a) return list list2 = [9, 4, 7, 1, 6] list3 = morflst(list2) print(list3)
23rd Jun 2018, 6:40 PM
Paul
Paul - avatar
+ 3
This is an example: def mklist(a, i): a = [x for x in range(0, i)] a.append(i) return a v =[] z =[] u = mklist (v, 6) y = mklist (z, 20) print(u) print(y)
22nd Jun 2018, 4:37 PM
Paul
Paul - avatar
+ 1
yes def function name ( parameters) " docstring" statement
22nd Jun 2018, 4:02 PM
Yuvraj Singh
Yuvraj Singh - avatar
+ 1
no, that doesn't help me. i need list to be an argument. something like: from random import choice list=[9, 4, 7, 1, 6] a=choice(list) list.remove(a) and i need last two lines to be executed multiple times on a multiple lists, in a single program, so those two lines must be a part of a def function. if possible, that is.
23rd Jun 2018, 5:58 PM
Igor Matić
Igor Matić - avatar
+ 1
thank you so much, Paul Jacobs! that should do the job, with some modifications.
23rd Jun 2018, 6:47 PM
Igor Matić
Igor Matić - avatar
0
didn't knew about return command.
23rd Jun 2018, 6:49 PM
Igor Matić
Igor Matić - avatar