Reputation: 25
I need to create a list that will be composed by substrings extracted for another list of strings. I tried a "for-loop" code, but I get unexpected results.
For example:
description = ['How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom',]
descrition_splitted = []
for i in description:
des = (i)
descrition_splitted.append((des.split(',')))
The lenght of description_splitted list is just 2 elements instead of 9 (number of substrings). It is like that each element contains many substrings.
What I am doing wrong?
Is another "for-loop" needed?
Upvotes: 1
Views: 72
Reputation: 119
try this
descrition_splitted=[]
for i in description:
descrition_splitted=descrition_splitted+i.split(',')
len(descrition_splitted)
output
9
Upvotes: 1
Reputation: 1333
If I guess your purpose right, you need to use +=
(list + list or extend a list with a list) instead of append
(insert item into list)
descrition_splitted += des.split(',')
Btw, we can refactor your code as:
description = [
'How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom', ]
descrition_splitted = []
for des in description:
descrition_splitted += des.split(',')
Shorter:
description = [
'How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom', ]
descrition_splitted = []
list(map(descrition_splitted.extend, (des.split(',') for des in description)))
Upvotes: 1
Reputation: 4427
You can use extend
instead of append
. This operates the same as the +=
mentioned in comments.
description = [
'How g... and focus',
'The s... chosen idiom'
]
comma_fragments = []
for segment in description:
comma_fragments.extend(segment.split(','))
if you want to try a list comprehension, you can even do
comma_fragments = [s for d in description for s in d.split(',')]
e.g.
>>> description = ['a,b', 'c,d,e']
>>> [s for d in description for s in d.split(',')]
['a', 'b', 'c', 'd', 'e']
Upvotes: 1
Reputation: 163207
You can use .extend
instead of .append
descrition_splitted.extend((i.split(',')))
Or you can loop the result of the split on ,
again to get 9 results.
description = ['How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom',]
descrition_splitted = []
for i in description:
for part in i.split(','):
descrition_splitted.append(part)
print(len(descrition_splitted))
Output
9
Upvotes: 1