0

I have a list with 2 levels and I would like to know how can I make it only 1 level list for me to use it as a column name.

My List:

[['great',
  'jeans',
  'wear',
  'comfortable',
  'pants',
  'perfect',
  'soft',
  'cute',
  'pair'],
 ['wear',
  'summer',
  'great',
  'perfect',
  'white',
  'tank',
  'shirt',
  'fabric',
  'underneath'],
 ['compliments',
  'color',
  'wore',
  'wear',
  'beautiful',
  'received',
  'got',
  'great',
  'bought'],
 ['sweater',
  'great',
  'soft',
  'nice',
  'color',
  'fabric',
  'material',
  'looks',
  'fits'],
 ['waist',
  'short',
  'just',
  'pants',
  'long',
  'little',
  'length',
  'petite',
  'wear'],
 ['store',
  'tried',
  'online',
  'saw',
  'sale',
  'just',
  'price',
  'try',
  'retailer'],
 ['small',
  'large',
  'medium',
  'wear',
  'ordered',
  'runs',
  'usually',
  'little',
  'big'],
 ['fabric',
  'skirt',
  'flattering',
  'just',
  'length',
  'cut',
  'color',
  'nice',
  'look']]

Expected Result:

["great,
  jeans,
  wear,
  comfortable,
  pants,
  perfect,
  soft,
  cute,
  pair"],
 ["wear,
  summer,
  great,
  perfect,
  white,
  tank,
  shirt,
  fabric,
  underneath"],
 ["compliments,
  color,
  wore,
  wear,
  beautiful,
  received,
  got,
  great,
  bought"],
 ["sweater,
  great',
  soft',
  nice',
  color',
  fabric',
  material',
  looks',
  fits"],
 ["waist,
  short,
  just,
  pants,
  long,
  little,
  length,
  petite,
  wear"],
 ["store,
  tried,
  online,
  saw,
  sale,
  just,
  price,
  try,
  retailer"],
 ["small,
  large,
  medium,
  wear,
  ordered,
  runs,
  usually,
  little,
  big"],
 ["fabric,
  skirt,
  flattering,
  just,
  length,
  cut,
  color,
  nice,
  look"]
Sergey Pugach
  • 5,561
  • 1
  • 16
  • 31
Maku
  • 1,476
  • 10
  • 21
  • 1
    You just removed the brackets from the first list, what exactly do you want? – Dani Mesejo Dec 19 '18 at 12:15
  • 1
    Possible duplicate of [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – buran Dec 19 '18 at 12:15
  • @buran: I don't see there's a flat list. It's rather joining lists into strings – offeltoffel Dec 19 '18 at 12:15
  • What type is your expected result? String? – ycx Dec 19 '18 at 12:16
  • @offeltoffel, in fact their example output does not make any sense. The question asks for "1 level list for me to use it as a column name". – buran Dec 19 '18 at 12:24
  • I believe he or she wants to combine the elements of the sublist to a single string. All the rest is just a deliberation on how to store these elements – offeltoffel Dec 19 '18 at 12:25

1 Answers1

1

If I understand correctly, you seek to combine single elements of your list into a string. This is done by "joining":

mylist = [['great', 'jeans', 'wear', 'comfortable', 'pants', 'perfect', 'soft', 'cute', 'pair'], 
          ['wear', 'summer', 'great', 'perfect', 'white', 'tank', 'shirt', 'fabric', 'underneath']]
result = []

for item in mylist:
    result.append(", ".join(item))

# output: ['great, jeans, wear, comfortable, pants, perfect, soft, cute, pair', 
#          'wear, summer, great, perfect, white, tank, shirt, fabric, underneath']

I changed your output a little. It will return a list that contains your desired strings. In your case each string was the only item of a new list, which doesn't make sense to me. If you do need them in single lists, put the join command in brackets [].

offeltoffel
  • 2,691
  • 2
  • 21
  • 35
  • `result = [", ".join(item) for item in mylist]` but I am sure tha't s not desired outcome. – buran Dec 19 '18 at 12:28
  • Why not? I found a for-loop easier to comprehend, but I personally would also go for list comprehension here. – offeltoffel Dec 19 '18 at 12:29