-3

I have a list in Python 2.7 that looks like this.

[[u'ABC'], [u'DEF'], [u'GHI']]

Now, I want to convert it so it should be like this.

['ABC', 'DEF', 'GHI']

How can I do this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Goshi
  • 3
  • 1
  • 1
    There are two components to this, have you researched either of them? – jonrsharpe May 06 '20 at 10:21
  • Not even sure what the second component is, this is really just flattening a list. It’s questionable why you think the leading `u` should be removed at all. – deceze May 06 '20 at 10:23
  • `[val[0] for val in [[u'ABC'], [u'DEF'], [u'GHI']]]` will do, but please move to Python 3! – Bruno Vermeulen May 06 '20 at 10:31

2 Answers2

0

If your list is named arr you can do arr = [i[0].encode('ascii') for i in arr]. Note this assumes that each element is a list of only (and at least) one item and that you aren't concerned about there being non-ASCII characters in the string

awarrier99
  • 3,628
  • 1
  • 12
  • 19
0

Welcome to Python. This is just flattening a list

flattened_list = [val[0] for val in [[u'ABC'], [u'DEF'], [u'GHI']]]
print(flattened_list)

By the way please move to Python 3!

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29