Logo

Programming-Idioms

History of Idiom 7 > diff from v142 to v143

Edit summary for version 143 by :
Restored version 141: No.

Version 142

2023-03-25, 02:08:25

Version 143

2023-03-26, 09:18:18

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Variables
i,x,items
Variables
i,x,items
Extra Keywords
indices traverse traversal
Extra Keywords
indices traverse traversal
Code
def getInvCount(arr, n):
    inv_count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (arr[i] > arr[j]):
                inv_count += 1
  
    return inv_count
Code
for i, x in enumerate(items):
    print i, x
Doc URL
https://docs.python.org/3/library/functions.html#enumerate
Doc URL
https://docs.python.org/3/library/functions.html#enumerate
Origin
https://stackoverflow.com/a/522578/871134
Origin
https://stackoverflow.com/a/522578/871134