Skip to content
Advertisement

is it possible to generate a list in list for desired numbers?

I wanted to generate a list in list :

now, I have basically two options, either I input the list through a text file or I should generate the list by itself.

is it possible to generate this type of list by itself using nested loops?

JavaScript

I wanted to put -1 at the place of the middle zero of each sub-list like [0, -1, 0], there are 5 sub-list so the -1 should be inserted 5 times

so desired result would be

JavaScript

In my actual work, I have 38 sub-lists, for convenience, I showed only 5 here.

my attempt –

presently I am doing this by using json.loads and inputting this as a dictionary then collecting it using append and further converting it into a list and then I’ll use those values. however, this method seems so cumbersome to me.

JavaScript

where unitvalue.txt contain

JavaScript

Advertisement

Answer

You can do it with a list comprehension:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement