Merlin
Merlin

Reputation: 25669

Python: Trim a list

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.

Upvotes: 0

Views: 2635

Answers (4)

NPE
NPE

Reputation: 500743

Here is another way:

In [5]: tuple(map(operator.itemgetter(0), Lt))
Out[5]: ('ABC', 'Abc', 'xyz', 'ABC', 'Abc')

Upvotes: 0

Merlin
Merlin

Reputation: 25669

solution:

r=zip(*Lt)[0]
print Lt

Upvotes: 3

momo
momo

Reputation: 1045

another way

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

>>> a
('ABC', 'Abc', 'xyz', 'ABC', 'Abc')

Upvotes: 0

Falmarri
Falmarri

Reputation: 48587

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


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

Upvotes: 5

Related Questions