-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
  • 107,083
  • 22
  • 201
  • 376
Ford
  • 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,396
  • 1
  • 9
  • 18
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,567
  • 2
  • 13
  • 23