+ 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.
6 Antworten
+ 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)
+ 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)
+ 1
yes
def function name ( parameters)
" docstring"
statement
+ 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.
+ 1
thank you so much, Paul Jacobs! that should do the job, with some modifications.
0
didn't knew about return command.