Home > Back-end >  Iterating through elements in a list from different index positions
Iterating through elements in a list from different index positions

Time:10-22

This should be an easy one but I have simply not come to a solution. This is the exercise:

Start with 4 words “comfortable”, “round”, “support”, “machinery”, return a list of all possible 2 word combinations.

Example: ["comfortable round", "comfortable support", "comfortable machinery", ...]

I have started coding a loop that would go through every element , starting with the element at index[0] :

words = ["comfortable, ", 'round, ', 'support, ', 'machinery, ']
index_zero= words[0]


for i in words:
    words = index_zero   i
    words_one = index_one   i 
    print(words)
>>> Output=
comfortable, comfortable,
comfortable, round,
comfortable, support,
comfortable, machinery

The issue is when I want to start iterating from the 2nd element ('round'). I have tried operating the indexes ( index[0] 1) but of course it won't return anything as the elements are strings. I know a conversion from string to indexes need to take place, but I'm not sure how.

I have also tried defining a function, but it will return None

word_list = ["comfortable, ", 'round, ', 'support, ', 'machinery, ']
index_change = word_list[0]  1

def word_variations(set_of_words):
    for i in set_of_words:
        set_of_words = set_of_words[0]   i


set_of_words = word_variations(word_list)   
print(set_of_words)

CodePudding user response:

I think this would do what you're looking for:

def word_variations(word_list):
  combinations = []
  for first_word in word_list:
    for second_word in word_list:
      if first_word != second_word:
        combinations.append(f'{first_word}, {second_word}')

  return combinations

word_list = ["comfortable", "round", "support", "machinery"]
print(word_variations(word_list))

Explanation:

You need to include a return statement at the end of the function to return a value. In my example function word_variations(), I first define an empty list called combinations. This will store each combination we compute. Then I iterate through all the words in the input word_list, create another inner loop to iterate through all words again, and if the first_word does not equal the second_word append the combination to my combinations list. Once all loops are complete, return the finished list from the function.

If I slightly change the code to print each of the results on a new line:

def word_variations(word_list):
  combinations = []
  for first_word in word_list:
    for second_word in word_list:
      if first_word != second_word:
        combinations.append(f'{first_word}, {second_word}')

  return combinations

word_list = ["comfortable", "round", "support", "machinery"]

for combo in word_variations(word_list):
  print(combo)

the output is:

comfortable, round
comfortable, support
comfortable, machinery
round, comfortable
round, support
round, machinery
support, comfortable
support, round
support, machinery
machinery, comfortable
machinery, round
machinery, support

CodePudding user response:

If you want to work with indexes in a Python loop like that, you should use either enumerate or iterate over the length of the list. The following examples will start the loop at the second element.

Example getting both index and the word at once with enumerate:

for i, word in enumerate(set_of_words[1:]):

Example using only indexes:

for i in range(1, len(set_of_words)):

Note: set_of_words[1:] above is a slice that returns the list starting at the second element.

CodePudding user response:

You can also use itertools.permutations() like this

from itertools import permutations

lst = ['comfortable', 'round', 'support', 'machinery']

for i in list(permutations(lst, 2)):
    print(i)
  • Related