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?










share|improve this question






















  • "one-liner" != Pythonic
    – juanpa.arrivillaga
    Nov 11 at 11:37















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?










share|improve this question






















  • "one-liner" != Pythonic
    – juanpa.arrivillaga
    Nov 11 at 11:37













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 9:46









Code Monkey

143110




143110












  • "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




"one-liner" != Pythonic
– juanpa.arrivillaga
Nov 11 at 11:37












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(", ")]





share|improve this answer























  • 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


















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']





share|improve this answer





















  • 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


















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))





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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(", ")]





share|improve this answer























  • 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















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(", ")]





share|improve this answer























  • 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













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(", ")]





share|improve this answer














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(", ")]






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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']





share|improve this answer





















  • 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















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']





share|improve this answer





















  • 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













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']





share|improve this answer












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']






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










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))





share|improve this answer





















  • 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















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))





share|improve this answer





















  • 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













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))





share|improve this answer












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))






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues