Aspire
Aspire

Reputation: 417

How do you append sequential elements when looping through a list?

I'm creating a bigram function, where the parameter is a list of words, and I want to return a new list that appends two consecutive words.

For example, if this was the text:

text = '''
it was the best of times
it was the worst of times
it was the age of wisdom
it was the age of foolishness
'''

I want the output to be:

[('it', 'was'), ('was', 'the'), ('the', 'best'), ('best', 'of'), ('of', 'times'), ('times', 'it'), ('it', 'was'), ('was', 'the'), ('the', 'worst'), ('worst', 'of'), ('of', 'times'), ('times', 'it'), ('it', 'was'), ('was', 'the'), ('the', 'age'), ('age', 'of'), ('of', 'wisdom'), ('wisdom', 'it'), ('it', 'was'), ('was', 'the'), ('the', 'age'), ('age', 'of'), ('of', 'foolishness')]

I know I need to loop through the list of words, but I'm unsure of how to append the consecutive elements together. I should note that I do not want to use any libraries or the zip function to solve this.

Here is what I have:

def bi_grams(tokens):
  bigram = []
  for word in tokens:
    bigram.append([[word], [???]])
  return bigram

The ??? part is where I'm stuck on. Any tips?

Upvotes: 2

Views: 347

Answers (2)

Umesh Jadhav
Umesh Jadhav

Reputation: 327

As you don't want to use zip function so,

Find my solution for you:

word_list = ['this', 'is', 'python', 'for', 'you']
result = []

for i in range(len(word_list)-1):
    result.append((word_list[i],word_list[i+1]))

print(result)

Result:

[('this', 'is'), ('is', 'python'), ('python', 'for'), ('for', 'you')]

As per your code:

def bi_grams(tokens):
    bigram = []
    for i in range(len(tokens)-1):
        bigram.append((tokens[i], tokens[i+1]))
    return bigram

You can always use zip function as easiest method as below:

bigram = list(zip(tokens, tokens[1:]))

Hope it helps.

Upvotes: 1

Selcuk
Selcuk

Reputation: 59297

Using zip is one of the easiest methods:

bigram = list(zip(tokens, tokens[1:]))

Upvotes: 2

Related Questions