-4

I am facing a problem with all my list items. The list shows like:

[('abc',), ('def',), ...]

I want the list to be like this:

['abc', 'def', ...]

How do I fix this?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 4
    Why did you put the items in tuples in the first place? – Barmar Feb 10 '20 at 11:52
  • 3
    The title of this question doesn't make any sense. You should update it. – Marco Bonelli Feb 10 '20 at 11:56
  • @MarcoBonelli it doesn't make sense *to you* because you are familiar with Python. I agree this should be rephrased though. – Corentin Pane Feb 10 '20 at 11:57
  • @CorentinPane how does it make sense in any way? Title says "How do I remove unwanted characters from my list items?", then the question that is asked is how to unpack a tuple. In other words, *nothing* needs to be removed, and it's not even clear what is "unwanted" or why. – Marco Bonelli Feb 10 '20 at 12:02
  • @Abhishek I feel sorry for you, hopefully you found the answer you wanted though. – Corentin Pane Feb 10 '20 at 12:04
  • @Marco Bonelli the asker has posted the output they are getting, and the output they are wanting. These outputs differ by a few characters, `(` `,)`, hence they are asking how to remove those characters which are unwanted – Hymns For Disco Feb 10 '20 at 12:10
  • @Abhishek I've updated the title for you to better reflect the wanted result, let me know if that helps. – Marco Bonelli Feb 10 '20 at 12:13

2 Answers2

4

Your list contains tuples of strings, not directly strings. They are not unnecessary characters, they indicate that you have tuples.

You could unpack every 1-uple in your list using list comprehension:

myList = [('abc',), ('def',)]

myNewList = [a[0] for a in myList] #['abc','def']
Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
  • additionally look at [this question](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) for "flattening" lists of lists – flurble Feb 10 '20 at 11:53
0

these are list of tuples you have iterate twice

list_ = [('abc',),('def',)]

for i in list_:

for j in i:
    print(j)

out put will be in single elements then you can make a new list and append these elements.