Iterate a List with Indexes in Python

Accessing the index in 'for' loops

Use the built-in function enumerate():

for idx, x in enumerate(xs):
print(idx, x)

It is non-pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable.

Check out PEP 279 for more.

Iterate a list with indexes in Python

>>> a = [3,4,5,6]
>>> for i, val in enumerate(a):
... print i, val
...
0 3
1 4
2 5
3 6
>>>

Iterating through elements in a list from different index positions

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

Loop through list with both content and index

Use the enumerate built-in function: http://docs.python.org/library/functions.html#enumerate

Multiple indexes iteration in Python's for loop

Come in late, all prev. posts have done great answering the question. Here is to sum up and answer - what if I want to different ordering on the 4 items:

>>> for value_tpl in zip(list1, list2, list3, list4):
n1, n2, n3, label = value_tpl
print(label, n3, n2, n1)


A 44 10 1
B 55 20 2
C 66 30 3

How to iterate a list accessing two indexes at a time and moving one index time after each iteration?

You could use a zip with an offset on the start and stop:

print(list(zip(a_list[:-1], a_list[1:])))

Slightly more flexible solution that doesn't rely on list syntax.

def special_read(data):
last = None
for curr in data:
if last is not None:
yield last, curr
last = curr

...

for a, b in special_read(a_list):
print(a, b)

Is it possible to only iterate through a certain index in a list of lists?

you could iterate over your lists of lists and access each inner lists at your wanted index:

my_index = <some integer>

for item in lists_of_lists:
doSomething(item[my_index])


Related Topics



Leave a reply



Submit