tags:

views:

68

answers:

2

I have this:

Lt = [('ABC', ), ('Abc', ), ('xyz', ), ('ABC', ), ('Abc', )]

I want this:

Lt = ('Abc', 'Abc', 'xyz', 'ABC', 'ABc')

remove the extra "(",")" and ",".... How do i do this.

+5  A: 

Is that a list of strings or tuples? Assuming they're tuples:


[t[0] for t in [('ABC', ), ('Abc', ), ('xyz', ), ('ABC', ), ('Abc', )]]

Falmarri
+1 Nice and elegant
Helper Method
For the sake of learning, this is called a "list comprehension" and thy are very efficient and easy to do in Python!
jathanism
didnt work, thk
@user, just apply `tuple`
aaronasterling
It gives me the last ('Abc',) missing everything else
[('ABC',), ('Abc',), ('xyz',), ('ABC',), ('Abc',)]>>> [t[0] for t in [('ABC', ), ('Abc', ), ('xyz', ), ('ABC', ), ('Abc', )]]['ABC', 'Abc', 'xyz', 'ABC', 'Abc']
João Pinto
@user428862: Could you be mistyping something perhaps? Because the code does work (I just copied it into my Python shell to verify).
David Zaslavsky
@user: You probably have the `[]` in the wrong place
Falmarri
A: 

another way

a = tuple([''.join(x) for x in Lt])

>>> a
('ABC', 'Abc', 'xyz', 'ABC', 'Abc')
momo
You can remove the square brackets in the call to `tuple`. a generator expression suffices because `tuple` will consume it.
aaronasterling