enter image description here enter image description here After some manipulations with a large list, I get the data I need in this form:

data print (data): TABLE CHAIR SOFA WALL 

Data types:

 <class 'str'> <class 'str'> <class 'str'> <class 'str'> 

You just need to make a list of them:

 mylist [TABLE, CHAIR, SOFA, WALL] 

Why do I have

 mylist = [h for h in data] 

and

 mylist = [] mylist.append (data) 

They do not give the desired result, but make lists of each word separately, but inside them break each letter and separate them with commas! How to make a normal list with the words (str) inside?

 print(repr(data)) 'TABLE' 'CHAIR' 'SOFA' 'WALL' 
  • I did not understand, show print(repr(data)) - andreymal
  • Again, I did not understand, print(repr(data)) cannot in any way output something, then you wrote it. Show screenshot of output - andreymal
  • What is not clear is that data is a few words. From them you need to make a list. An ordinary list of words separated by a comma. - Kirill
  • “A few words” is a very vague concept that can cover a bunch of types. And str is not a few words, it is an array of characters, therefore either data is not of type str, or you do not agree with something. repr line cannot produce a result in several lines (four, as you have shown) - repr always gives exactly one line without hyphenation. Show a screenshot of the print(repr(data)) output, but rather give a minimal reproducible example , because nothing is clear at all. - andreymal
  • Especially since you in your question showed two ways to create a list that are exactly working. Why they do not work for you - it is not clear, show all the necessary code to reproduce the problem so that you can answer something intelligible - andreymal

3 answers 3

 mylist = [] for h in a: q = h + 'LED' mylist.append(q) print(mylist) 
  • It turned out like this: ['GASLED'] ['USDTLED'] .. all words individually became lists .. - Kirill
  • This can't happen like this from my code. Check if you copied it correctly. - andreymal
  • @ Kirill, look, it works: i.stack.imgur.com/35rzl.png - andreymal
  • Incorrectly in your screenshot. Baseline data is not a, aq! - Kirill
  • one
    @Kirill Then I have to repeat it again: give a minimal reproducible example with your non-working code. I rewrote the code from your screenshot into my answer - it works, and the original data in your screenshot was exactly a. If you lied in your screenshot - please stop lying and show the real code, if you want to get the same help :) - andreymal

I do not understand what the problem may be, everything seems to be simple.

Regular list of comma separated words

 data = data.split(',') 

    In your data variable, there is probably only one line in which the words are separated by a special symbol \n - by switching to a new line:

     data = 'TABLE\nCHAIR\nSOFA\nWALL' print(data) 
     TABLE CHAIR SOFA WALL 

    Then you can use the split() method to translate it into a list:

     mylist = data.split() print(mylist) 
     ['TABLE', 'CHAIR', 'SOFA', 'WALL']