-1

I have a list of lists that looks like this mylst

[[('Salmon', 9), ('Fish&Chips', 3), ('Pasta', 8), ('Shrimp', 10)],
 [('Shrimp', 8), ('Fish&Chips', 10), ('Salmon', 9), ('Pasta', 7)],
 [('Shrimp', 10), ('Fish&Chips', 6), ('Salmon', 8), ('Pasta', 5)],
 [('Shrimp', 7), ('Pasta', 9), ('Salmon', 8), ('Fish&Chips', 8)],
 [('Fish&Chips', 10), ('Shrimp', 8), ('Salmon', 9), ('Pasta', 3)]]

I can access the sublists if I print the desired index

mylst[0]

which returns

[('Salmon', 9), ('Fish&Chips', 3), ('Pasta', 8), ('Shrimp', 10)]

But I would like to store each sublist in a new list.

Because eventually I would like to extract the items(tuple index 0) and the quantity ordered (tuple index 1) for each sublist and store them in two separate lists. But I can't do this if I can't split the lists

I tried to iterate through the main list

newlst=[]

for sublst in mylst:
    newlst.append(sublst)

and

newlst=[]

for i in range(len(mylst)):
    for sublst in mylst:
        newlst.append(mylst[i])

I am not sure how to split then store multiple outputs from each iteration of the for loop.

The desired output is to have the 5 sublists as seperate lists. For example

lst1=[('Salmon', 9), ('Fish&Chips', 3), ('Pasta', 8), ('Shrimp', 10)]

lst2 = [('Shrimp', 8), ('Fish&Chips', 10), ('Salmon', 9), ('Pasta', 7)]

and so on.

note: my question is not duplicate of this one, as I have read the solutions. I want to store my sublists in seperate lists. which is different.

leena
  • 563
  • 1
  • 8
  • 25
  • 1
    What is the expected output? – Chris Sep 30 '19 at 02:23
  • 5 lists. Each one contains the values of one of the sublist. – leena Sep 30 '19 at 02:24
  • 2
    You mean something like `a,b,c,d,e = mylst` (which is quite inefficient way of dealing multiple lists btw) – Chris Sep 30 '19 at 02:25
  • What happens if you have more than five lists? – Dani Mesejo Sep 30 '19 at 02:27
  • I am looking for a general solution. A for loop that can iterate through the main list according the number of sublists the main list contains. – leena Sep 30 '19 at 02:28
  • 2
    Why do you need *lst1*, *lst2*, ..., when you already have `mylst[0]`, `mylst[1]`, ...? That's bad design. What would happen if *mylst* contained 1000 sublists? Having *lst1* ... *lst1000* would **make absolutely no sense**. – CristiFati Sep 30 '19 at 02:35
  • 1
    As others have said, I think OP should re-evaluate whether what's being asked for makes any sense. Essentially, you want 5 variables identified by letters. But that's the whole point of a list--a collection of 5 variables identified by numbers. It's much more convenient that way. If you really need letters, use a dict, but creating separate variables makes it harder to work with, not easier. – ggorlen Sep 30 '19 at 02:35
  • 1
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – ggorlen Sep 30 '19 at 02:38

2 Answers2

0

If you just want to unpack the list then you can do what Chris said above and just do a,b,c,d,e = mylst, but if what you want is to repackage the data in a more usable way, then I suggest storing this in a pandas dataframe. You can do that with the following code

import numpy as np
import pandas as pd

my_df = pd.DataFrame(np.array(mylst).reshape(-1, 2),
    columns=['item', 'quantity']) 
Kyle Safran
  • 463
  • 3
  • 8
  • the main list I provided is extracted from a dataframe. Each sublist is a row of one column in the dataframe. I would like to save each row from the specified column to seperate list. – leena Sep 30 '19 at 03:13
  • Like the comments above say, you might want to re-evaluate your strategy here. You can unpack the list `lst1, lst2, lst3 ... = mylst` but that is a bad design pattern. Pandas dataframes are built for handling this sort of data, and it's likely you can use that framework to solve your problem much more efficiently – Kyle Safran Sep 30 '19 at 14:14
  • Thank you Kyle. I will try to do this. – leena Sep 30 '19 at 22:12
0

You can make one massive nested list by using a for loop each containing the desired list.

for each in lst:
seplist = [] #splitted list of each digit of every integer
for eachagain in str(each):
    seplist.append(integer)
nestedlist.append(seplist)

Here the nested list can be separated and stored by using another for loop

Akhilesh Magdum
  • 324
  • 2
  • 13