2

I have a list that is in a similar format as this:

list1 = ['random words go', 'here and','the length varies','blah',
         'i am so confused', 'lala lala la']

What code would be appropriate to return every 3rd item of the list, including the first word? This is the expected output:

["random", "here", "length", "i", "confused", "la"]

I am thinking that I should use the split function, but I don't know how to do this. Can someone also explain how I can make it so the whole list isn't in 'parts' like that? Rather, how can I turn it into one long list, if that makes sense.

4
  • possible duplicate of Pythonic way to return list of every n'th item in a larger list Commented Jul 17, 2013 at 0:14
  • @dnozay not exactly, the OP is explaining it poorly though. Commented Jul 17, 2013 at 0:17
  • yes, I see that now, the other question is still very relevant for one part of the problem. Commented Jul 17, 2013 at 0:43
  • What he really wants is to join each phrase as a string, separate by words again into a list, then pick out certain ones. Poorly worded. Commented Jul 17, 2013 at 1:24

6 Answers 6

6

This is probably the most readable way to do it:

>>> list1 = ['random words go', 'here and','the length varies','blah', 'i am so confused', 'lala lala la']
>>> result = ' '.join(list1).split()[::3]
['random', 'here', 'length', 'i', 'confused', 'la']

or without joining and splitting the list again:

from itertools import chain
result = list(chain.from_iterable(s.split() for s in list1))[::3]

then you can just join the result:

>>> ' '.join(result)
'random here length i confused la'
Sign up to request clarification or add additional context in comments.

1 Comment

@BedSheets ' '.join(...)
3

Go for:

list1 = ['random words go', 'here and','the length varies','blah', 'i am so confused', 'lala lala la']

from itertools import chain, islice    
sentence = ' '.join(islice(chain.from_iterable(el.split() for el in list1), None, None, 3))
# random here length i confused la

Comments

1
[word for item in list1 for word in item.split()][0::3]

Comments

0

Another memory efficient version

>>> list1 = ['random words go', 'here and','the length varies','blah', 'i am so confused', 'lala lala la']
>>> from itertools import chain, islice
>>> ' '.join(islice(chain.from_iterable(map(str.split, list1)), 0, None, 3))
'random here length i confused la'

Comments

0

To turn it into one long list:

>>> list6 = []
>>> for s in list1:
...   for word in s.split():
...     list6.append(word)
... 
>>> list6
['random', 'words', 'go', 'here', 'and', 'the', 'length', 'varies',     'blah', 'i', 'am', 'so', 'confused', 'lala', 'lala', 'la']
>>> 

Then you can do the slicing as suggested with [::3]

>>> list6[::3]
['random', 'here', 'length', 'i', 'confused', 'la']

If you want it in one string:

>>> ' '.join(list6[::3])
'random here length i confused la'
>>>>

Comments

0

Try this:

>>> list1 = ['random words go', 'here and','the length varies','blah', 'i am so confused', 'lala lala la']
>>> result = ' '.join(list1).split()[::3]
['random', 'here', 'length', 'i', 'confused', 'la']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.