A more Pythonic one-liner solution?
up vote
0
down vote
favorite
I am looking for a more Pythonic one-liner to split and flatten lists. The original list looks like this:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
With the above un-processed list, I need to transform it to the following processed list:
negative_list = ['apple strudel', 'apple', 'orange', 'pear ice cream']
You will notice that 'apple', 'orange', 'pear ice cream' have been split into individual items in the transformed list.
I wrote the following:
negative_list =
negatives =
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = [item for sublist in negative_list for item in sublist]
for i in negative_list:
if ',' not in i: negatives.append(i.strip())
else:
for element in i.split(','): negatives.append(element.strip())
print(negative_list)
print(negatives)
I tried writing a Pythonic one-liner without declaring so many variables, but with little success. Could someone help?
python list
add a comment |
up vote
0
down vote
favorite
I am looking for a more Pythonic one-liner to split and flatten lists. The original list looks like this:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
With the above un-processed list, I need to transform it to the following processed list:
negative_list = ['apple strudel', 'apple', 'orange', 'pear ice cream']
You will notice that 'apple', 'orange', 'pear ice cream' have been split into individual items in the transformed list.
I wrote the following:
negative_list =
negatives =
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = [item for sublist in negative_list for item in sublist]
for i in negative_list:
if ',' not in i: negatives.append(i.strip())
else:
for element in i.split(','): negatives.append(element.strip())
print(negative_list)
print(negatives)
I tried writing a Pythonic one-liner without declaring so many variables, but with little success. Could someone help?
python list
"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am looking for a more Pythonic one-liner to split and flatten lists. The original list looks like this:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
With the above un-processed list, I need to transform it to the following processed list:
negative_list = ['apple strudel', 'apple', 'orange', 'pear ice cream']
You will notice that 'apple', 'orange', 'pear ice cream' have been split into individual items in the transformed list.
I wrote the following:
negative_list =
negatives =
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = [item for sublist in negative_list for item in sublist]
for i in negative_list:
if ',' not in i: negatives.append(i.strip())
else:
for element in i.split(','): negatives.append(element.strip())
print(negative_list)
print(negatives)
I tried writing a Pythonic one-liner without declaring so many variables, but with little success. Could someone help?
python list
I am looking for a more Pythonic one-liner to split and flatten lists. The original list looks like this:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
With the above un-processed list, I need to transform it to the following processed list:
negative_list = ['apple strudel', 'apple', 'orange', 'pear ice cream']
You will notice that 'apple', 'orange', 'pear ice cream' have been split into individual items in the transformed list.
I wrote the following:
negative_list =
negatives =
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = [item for sublist in negative_list for item in sublist]
for i in negative_list:
if ',' not in i: negatives.append(i.strip())
else:
for element in i.split(','): negatives.append(element.strip())
print(negative_list)
print(negatives)
I tried writing a Pythonic one-liner without declaring so many variables, but with little success. Could someone help?
python list
python list
asked Nov 11 at 9:46
Code Monkey
143110
143110
"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37
add a comment |
"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37
"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37
"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37
add a comment |
3 Answers
3
active
oldest
votes
up vote
-1
down vote
accepted
You can try this solution, although this is not recommended for production code:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = sum([elem.split(", ") for elem in negative_list[0]],)
print(negative_list)
Output:
['apple strudel', 'apple', 'orange', 'pear ice cream']
Another way is to use a nested for
loop with list-comprehension
:
negative_list = [elem.strip() for item in negative_list[0] for elem in item.split(", ")]
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
|
show 2 more comments
up vote
2
down vote
Use itertools.chain.from_iterable
with a generator expression:
from itertools import chain
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
print(list(chain.from_iterable(x.split(', ') for x in negative_list[0])))
# ['apple strudel', 'apple', 'orange', 'pear ice cream']
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
add a comment |
up vote
-1
down vote
I think this one resolves the problem
x = [['apple strudel', 'apple, orange, pear ice cream'], ["test", "test1, test2, test3"]]
def flatten(x):
return sum([(x.split(", ")) for x in sum(x, )], )
print(flatten(x))
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
You can try this solution, although this is not recommended for production code:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = sum([elem.split(", ") for elem in negative_list[0]],)
print(negative_list)
Output:
['apple strudel', 'apple', 'orange', 'pear ice cream']
Another way is to use a nested for
loop with list-comprehension
:
negative_list = [elem.strip() for item in negative_list[0] for elem in item.split(", ")]
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
|
show 2 more comments
up vote
-1
down vote
accepted
You can try this solution, although this is not recommended for production code:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = sum([elem.split(", ") for elem in negative_list[0]],)
print(negative_list)
Output:
['apple strudel', 'apple', 'orange', 'pear ice cream']
Another way is to use a nested for
loop with list-comprehension
:
negative_list = [elem.strip() for item in negative_list[0] for elem in item.split(", ")]
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
|
show 2 more comments
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
You can try this solution, although this is not recommended for production code:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = sum([elem.split(", ") for elem in negative_list[0]],)
print(negative_list)
Output:
['apple strudel', 'apple', 'orange', 'pear ice cream']
Another way is to use a nested for
loop with list-comprehension
:
negative_list = [elem.strip() for item in negative_list[0] for elem in item.split(", ")]
You can try this solution, although this is not recommended for production code:
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
negative_list = sum([elem.split(", ") for elem in negative_list[0]],)
print(negative_list)
Output:
['apple strudel', 'apple', 'orange', 'pear ice cream']
Another way is to use a nested for
loop with list-comprehension
:
negative_list = [elem.strip() for item in negative_list[0] for elem in item.split(", ")]
edited Nov 11 at 10:01
answered Nov 11 at 9:51
Vasilis G.
2,9732721
2,9732721
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
|
show 2 more comments
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
Thank you for that, but what do you mean by 'production code'?
– Code Monkey
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
In case you want to ship your program for others to use it. To commercially publish it.
– Vasilis G.
Nov 11 at 9:53
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
Do you say that because it is too difficult to understand? It is clever but I certainly find it difficult to understand!
– Code Monkey
Nov 11 at 9:56
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
This may be asking too much - but could you explain the 'negative_list[0]' bit? How do you manage to access all elements of the list by calling the very first element alone?
– Code Monkey
Nov 11 at 10:37
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
|
show 2 more comments
up vote
2
down vote
Use itertools.chain.from_iterable
with a generator expression:
from itertools import chain
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
print(list(chain.from_iterable(x.split(', ') for x in negative_list[0])))
# ['apple strudel', 'apple', 'orange', 'pear ice cream']
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
add a comment |
up vote
2
down vote
Use itertools.chain.from_iterable
with a generator expression:
from itertools import chain
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
print(list(chain.from_iterable(x.split(', ') for x in negative_list[0])))
# ['apple strudel', 'apple', 'orange', 'pear ice cream']
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
add a comment |
up vote
2
down vote
up vote
2
down vote
Use itertools.chain.from_iterable
with a generator expression:
from itertools import chain
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
print(list(chain.from_iterable(x.split(', ') for x in negative_list[0])))
# ['apple strudel', 'apple', 'orange', 'pear ice cream']
Use itertools.chain.from_iterable
with a generator expression:
from itertools import chain
negative_list = [['apple strudel', 'apple, orange, pear ice cream']]
print(list(chain.from_iterable(x.split(', ') for x in negative_list[0])))
# ['apple strudel', 'apple', 'orange', 'pear ice cream']
answered Nov 11 at 9:55
Austin
8,8643828
8,8643828
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
add a comment |
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
This. No reason to check if comma is in the string, as split will return a list of length zero when it doesn’t.
– soundstripe
Nov 11 at 16:57
add a comment |
up vote
-1
down vote
I think this one resolves the problem
x = [['apple strudel', 'apple, orange, pear ice cream'], ["test", "test1, test2, test3"]]
def flatten(x):
return sum([(x.split(", ")) for x in sum(x, )], )
print(flatten(x))
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
add a comment |
up vote
-1
down vote
I think this one resolves the problem
x = [['apple strudel', 'apple, orange, pear ice cream'], ["test", "test1, test2, test3"]]
def flatten(x):
return sum([(x.split(", ")) for x in sum(x, )], )
print(flatten(x))
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
add a comment |
up vote
-1
down vote
up vote
-1
down vote
I think this one resolves the problem
x = [['apple strudel', 'apple, orange, pear ice cream'], ["test", "test1, test2, test3"]]
def flatten(x):
return sum([(x.split(", ")) for x in sum(x, )], )
print(flatten(x))
I think this one resolves the problem
x = [['apple strudel', 'apple, orange, pear ice cream'], ["test", "test1, test2, test3"]]
def flatten(x):
return sum([(x.split(", ")) for x in sum(x, )], )
print(flatten(x))
answered Nov 11 at 9:58
Eternal_flame-AD
3126
3126
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
add a comment |
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
sum to flatten lists is an anti-pattern
– juanpa.arrivillaga
Nov 11 at 11:36
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
But I think this is the only way to flatten this in one line as asked in the question
– Eternal_flame-AD
Nov 11 at 12:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247492%2fa-more-pythonic-one-liner-solution%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37