-2

I have this list of lists named Restaurant_Type:

Restaurant_Type = [['Dessert Parlor', 'Quick Bites'],
                   ['Casual Dining', 'Bar'],
                   ['Casual Dining', 'Bar'],
                   ['Fine Dining'],
                   ['Casual Dining', 'Bar']]

of 180 rows. I want to loop through each list and enter every value in a new list. For example,

new_list = ['Dessert Parlor', 'Quick Bites', 'Casual Dining', 'Bar','Casual Dining',
            'Bar',.....]

So that I can calculate the frequency of each restaurant type.

I tried using for loop but I'm getting list of words like this,

  "'",
  'D',
  'e',
  's',
  's',
  'e',
  'r',
  't',
  ' ',
  'P',
  'a',
  'r',
  'l',
  'o',
  'r',
  ...
martineau
  • 119,623
  • 25
  • 170
  • 301
Devesh
  • 127
  • 1
  • 10
  • 2
    Could you, please, add your code. From the output it seems that your are iterating over the string itself. Also please be more specific about your input, exactly what do you mean by `column of lists`? – Dani Mesejo Oct 16 '19 at 20:23
  • We aren't sure what you mean. The "column of lists" you posted is not legal Python syntax; `column` is not a built-in type. – Prune Oct 16 '19 at 20:25
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. – Prune Oct 16 '19 at 20:26
  • Regardless of what code you've written, you might want to look into `chain` from [itertools](https://docs.python.org/2/library/itertools.html#itertools.chain). – Brian Oct 16 '19 at 20:26
  • You basically have a 2D list to flatten, but my guess is that you're treating at as if it's 3D, causing the last iteration to be over the letters of the strings. Without seeing your code, hard to know – Ofer Sadan Oct 16 '19 at 20:26

1 Answers1

0

try something like this:

 Restaurant_Type = [['Dessert Parlor', 'Quick Bites'], ['Casual Dining', 'Bar'], ['Casual Dining', 'Bar'],['Fine Dining'],['Casual Dining', 'Bar']]


 new_list = []
 for res in Restaurant_Type: 
     new_list.extend(res)
 print(new_list)

the output should be like this:

['Dessert Parlor', 'Quick Bites', 'Casual Dining', 'Bar', 'Casual Dining', 'Bar', 'Fine Dining', 'Casual Dining', 'Bar']
Oak__
  • 67
  • 7