Home > Software engineering >  Add elements in a list at the n index of a other list
Add elements in a list at the n index of a other list

Time:05-15

calibres_prix =['115-135', '1.87'], ['136-165', '1.97'], ['150-180', '1.97'], 
    ['190-220', '1.97'], ['80-95', '1.42'], ['95-115', '1.52'], ['150-180', '1.82'], 
    ['115-135', '1.72'], ['136-165', '1.82'], ['150-180', '1.82'], ['190-220', '1.82'], 
    ['80-95', '1.42'], ['95-115', '1.72'], ['115-135', '1.92'],  ['136-165', '2.02'], 
    ['150-180', '2.02'], ['190-220', '2.02'], ['80-95', '1.27'], ['95-115', '1.57'],
    ['115-135', '1.77'], ['136-165', '1.87'],  ['150-180', '1.87'], ['190-220', '1.87'], 
    ['80-95', '1.37'], ['95-115', '1.67'], ['115-135', '1.87'], ['136-165', '1.97'],
    ['190-220', '1.82'], ['150-180', '1.97'], ['190-220', '1.97'], ['80-95', '1.22'], 
    ['95-115', '1.45'], ['115-135', '1.65'], ['136-165', '1.82'], ['95-115', '1.52']......


varieties=["GOLDEN","GALA","OPAL","GRANNY","CANADE GRISE", "PINK ROSE",
           "CHANTECLER","RED","GOLDRESH","BRAEBURN","STORY"]

Hi everbody, I want to insert to the list calibres_prix the name of the variety. To the calibres_prix [0:23] the varieties[0], to the calibres_prix [24:47] the varieties[1], etc .... i have 264 lists for calibres_prix

I want this output :

['GOLDEN','115-135', '1.87']
....
['GALA','190-220', '1.97']

I try this:

for i in range(0,len(calibres_prix),24):
    j=i 24
    for l in range(0,len(varieties),1):
        for c in calibres_prix[i:j]:
            c.insert(0,varieties[l])

CodePudding user response:

Close! You are appending all the varieties to each list in your loop, however.

You could try this instead:

for i in range(0, len(calibres_prix)):
    calibres_prix[i] = [varieties[i//24]]   calibres_prix[i]

For each list in calibres_prix (I have assumed this is a list of lists, as you haven't included an initial [), add the i//24 th varieties value at the start.

CodePudding user response:

You can take advantage of zip(), but to do so you'd need to repeat all the elements of varieties 24 times:

varieties_repeat_24 = sum(([v]*24 for v in varieties), [])

desired_output = [(variety, *nums) for nums, variety in zip(calibres_prix, varieties_repeat_24)]

Just note that this is a bit wasteful in terms of memory. There are much better solutions... have you considered using Pandas?

CodePudding user response:

First of all, you should make a list from calibres_prix that only contains objects that will be bounded with varieties ones. THen, you can use the zip() function to do the work.

table = zip(varieties, calibres_prix)
for variety, prix in table:
    i = [variety, prix[0], prix[1]]
    print(i)

This will be your output :

GOLDEN : ['115-135', '1.87']
GALA : ['136-165', '1.97']
OPAL : ['150-180', '1.97']
GRANNY : ['190-220', '1.97']
CANADE GRISE : ['80-95', '1.42']
PINK ROSE : ['95-115', '1.52']
CHANTECLER : ['150-180', '1.82']
RED : ['115-135', '1.72']
GOLDRESH : ['136-165', '1.82']
BRAEBURN : ['150-180', '1.82']
STORY : ['190-220', '1.82']

For further information, you can check this post and the documenation

  • Related