+ 4
L=[1,2,3,4,5,[5,6,7],85,96] so remove inside list squre bracktes how?
output is [1,2,3,4,5,5,6,7,8,85,96]
12 odpowiedzi
+ 4
Hi Lokesh. Much more advanced way of tackling this problem is to use the recursion method.
https://code.sololearn.com/caH5zTZmlcZV/#py
# function: list_value_extract
#
# This function let you extract all the elements values of nested lists into a new list.
# @param int|string|list param
# @param list ext_list
# return void
def list_value_extract (param, ext_list = []):
# enter if the supplied param is a list
if (type (param) is list):
# loop through the list and process it
for value in param:
# if the list element is a list again, then use recursion method
if (type (value) is list):
list_value_extract (value)
else:
# if the list element is a simple value, then append to the new list
ext_list.append(value)
# enter if the supplied is a single value
else:
ext_list.append(param)
# return the extracted values array
return ext_list;
L = [1,2,3,4,5,[5,[6],7],85,96]
print (list_value_extract(L))
+ 2
𝕾𝖆𝖌𝖆𝕿𝖍𝖊𝕲𝖗𝖊𝖆𝖙 💯, as requested. This is what KrOW was describing.
This will expand the embedded lists in place and will handle multiple layers deep as well.
https://code.sololearn.com/cB0Ts19S5KdS/?ref=app
+ 1
this will work:
https://code.sololearn.com/cMHkvrqVB309/?ref=app
+ 1
1) create another empty list
2) loop on original list items
3) for any item if it its not a list add to copy list, else repeat from poin 1) but using this item like original
+ 1
tqs pulkit..
+ 1
𝕾𝖆𝖌𝖆𝕿𝖍𝖊𝕲𝖗𝖊𝖆𝖙 💯 Sure... You have to take cure of current index in iteration and "inflate" new items with it
Omar Sarr OPEN A NEW DISCUSSION for different problen please
+ 1
1_coder nice code. You deserve best answer bedge for this question. for making this program more user firendly take input list from user using eval. after that your program is complete.
Thanks for the code. A perfect code. 💯
0
Pulkit Kamboj KrOW can't we do it on same list?
0
By the way Omar Sarr this can give you some idea of what you want to do
https://code.sololearn.com/c58a7JighZSz/?ref=app
0
A NEW APPROACH
one liner✓✓✓✓✓.
it can even merge multi-dimensional lists
https://code.sololearn.com/cyMbKWg2E504/?ref=app
- 1
Hello,
Please, can someone help me solve this problemWrite a program that will take in a user input, this input will determine how many times the dice will roll, and will use two random numbers to "roll" the dice, after each roll, please output the to random numbers for each roll.
Ex. input a 2 and
Roll 1, die1= 6 die2=3
Roll 2, die1=3 die2=1
The range of the random numbers should be between 1-6.