You didn't say what you mean by "best", but presumably you mean "most pythonic" or "most readable" or something like that.
The list comprehension given by F3AR3DLEGEND is probably the simplest. Anyone who knows how to read a list comprehension will immediately know what it means.
y = [i[0] for i in x]
However, often you don't really need a list, just something that can be iterated over once. If you've got a billion elements in x
, building a billion-element y
just to iterate over it one element at a time may be a bad idea. So, you can use a generator expression:
y = [i[0] for i in x]
If you prefer functional programming, you might prefer to use map
. The downside of map
is that you have to pass it a function, not just an expression, which means you either need to use a lambda
function, or itemgetter
:
y = map[operator.itemgetter[0], x]
In Python 3, this is equivalent to the generator expression; if you want a list
, pass it to list
. In Python 2, it returns a
list
; if you want an iterator, use itertools.imap
instead of map
.
If you want a more generic flattening solution, you can write one yourself, but it's always worth looking at itertools
for generic solutions of this kind, and there is in fact a recipe called flatten
that's used to "Flatten one level of nesting". So, copy and paste that into your code [or pip install more-itertools
] and you can
just do this:
y = flatten[x]
If you look at how flatten
is implemented, and then at how chain.from_iterable
is implemented, and then at how chain
is implemented, you'll notice that you could write the same thing in terms of builtins. But why bother, when flatten
is going to be more readable and obvious?
Finally, if you want to reduce the generic version to a nested list comprehension [or generator expression, of course]:
y = [j for i in x for j in i]
However, nested list comprehensions are very easy to get wrong,
both in writing and reading. [Note that F3AR3DLEGEND, the same person who gave the simplest answer first, also gave a nested comprehension and got it wrong. If he can't pull it off, are you sure you want to try?] For really simple cases, they're not too bad, but still, I think flatten
is a lot easier to read.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Let’s discuss certain ways in which this task is performed. Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of list is by using re.
Python3
import
re
lst
=
[[
11
,
100
], [
22
,
200
], [
33
,
300
], [
44
,
400
], [
88
,
800
]]
temp
=
re.sub[r
'[\[\]\[\], ]'
, '',
str
[lst]]
Output
=
[
int
[i]
for
i
in
set
[temp]]
print
["Initial
List
is
:", lst]
print
["Output
list
is
:", Output]
Output:
Initial List is : [[11, 100], [22, 200], [33, 300], [44, 400], [88, 800]] Output list is : [1, 4, 8, 0, 3, 2]
Method #2: Using itertools.chain[] and lambda[] This is yet another way to perform this particular task using lambda[].
Python3
from
itertools
import
chain
lst
=
[[
11
,
100
], [
22
,
200
], [
33
,
300
], [
44
,
400
], [
88
,
800
]]
temp
=
map
[
lambda
x:
str
[x], chain.from_iterable[lst]]
Output
=
set
[]
for
x
in
temp:
for
elem
in
x:
Output.add[elem]
print
["Initial
List
is
:", lst]
print
["Output
list
is
:", Output]
Output:
Initial List is : [[11, 100], [22, 200], [33, 300], [44, 400], [88, 800]] Output list is : {‘8’, ‘4’, ‘0’, ‘2’, ‘1’, ‘3’}
Method #3: Using list[],map[],join[] and set[] methods
Initially we convert list containing tuple of integers to list containing tuple of strings.Later we will concatenate the string[obtained by joining tuple strings] to an empty string.And then use set to remove duplicates.Finally convert them to integer type and print output list
Python3
lst
=
[[
11
,
100
], [
22
,
200
], [
33
,
300
], [
44
,
400
], [
88
,
800
]]
p
=
""
for
i
in
lst:
x
=
list
[
map
[
str
,i]]
p
+
=
"".join[x]
p
=
list
[
map
[
int
,
set
[p]]]
print
[
"Initial List is :"
, lst]
print
[
"Output list is :"
, p]
Output
Initial List is : [[11, 100], [22, 200], [33, 300], [44, 400], [88, 800]] Output list is : [0, 4, 2, 3, 8, 1]