Skip to main content
added 599 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240

Tested on Python 3.12

Tested on Python 3.12

Here'sHere are twelve examples of how you can access the indices with their corresponding array's elements using for loops, while loops and some looping functions. Note that array indices always start from zero by default (see example 4 to change this).

items = [8, 23, 45, 12, 78]

for i, item in enumerate(items, start=1000start=100):
    print(i, item)
Result:

#    1000100 8
#    1001101 23
#    1002102 45
#    1003103 12
#    1004104 78
Result:

#    0  8
#    1  23
#    2  45
#    3  12
#    4  78

12. Also, it's nice to iterate list's elements inside class' Static Method.

items = [8, 23, 45, 12, 78]

class ElementPlus:
    @staticmethod                            # decorator
    def indexForEachOfMy(iterable):
        for pair in enumerate(iterable):
            print pair

ElementPlus.indexForEachOfMy(items)

Result:

#    (0, 8)
#    (1, 23)
#    (2, 45)
#    (3, 12)
#    (4, 78)

Tested on Python 3.12

Here's how you can access the indices with their corresponding array's elements using for loops, while loops and some looping functions.

items = [8, 23, 45, 12, 78]

for i, item in enumerate(items, start=1000):
    print(i, item)
Result:

#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78
Result:

#    0  8
#    1  23
#    2  45
#    3  12
#    4  78

Tested on Python 3.12

Here are twelve examples of how you can access the indices with their corresponding array's elements using for loops, while loops and some looping functions. Note that array indices always start from zero by default (see example 4 to change this).

items = [8, 23, 45, 12, 78]

for i, item in enumerate(items, start=100):
    print(i, item)
Result:

#    100 8
#    101 23
#    102 45
#    103 12
#    104 78
Result:

#    0  8
#    1  23
#    2  45
#    3  12
#    4  78

12. Also, it's nice to iterate list's elements inside class' Static Method.

items = [8, 23, 45, 12, 78]

class ElementPlus:
    @staticmethod                            # decorator
    def indexForEachOfMy(iterable):
        for pair in enumerate(iterable):
            print pair

ElementPlus.indexForEachOfMy(items)

Result:

#    (0, 8)
#    (1, 23)
#    (2, 45)
#    (3, 12)
#    (4, 78)
deleted 15 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240

Tested on Python 3.12Tested on Python 3.12

Result:

 
Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

Result:

 
Result:

#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)

Result:

 
Result:

#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78

Result:

 
Result:

#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78

Result:

 
Result:

#    ('Index:', 0, 'Value:', 8)
#    ('Index:', 1, 'Value:', 23)
#    ('Index:', 2, 'Value:', 45)
#    ('Index:', 3, 'Value:', 12)
#    ('Index:', 4, 'Value:', 78)

Result:

 
Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

Result:

 
Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

Result:

 
Result:

#    (0, 8)
#    (1, 23)
#    (2, 45)
#    (3, 12)
#    (4, 78)

Result:

 
Result:

#    [(0, 8), (1, 23), (2, 45), (3, 12), (4, 78)]

Result:

 
Result:

#    0: 8
#    1: 23
#    2: 45
#    3: 12
#    4: 78

Result:

 
Result:

#    0  8
#    1  23
#    2  45
#    3  12
#    4  78

Tested on Python 3.12

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

Result:

#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)

Result:

#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78

Result:

#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78

Result:

#    ('Index:', 0, 'Value:', 8)
#    ('Index:', 1, 'Value:', 23)
#    ('Index:', 2, 'Value:', 45)
#    ('Index:', 3, 'Value:', 12)
#    ('Index:', 4, 'Value:', 78)

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

Result:

#    (0, 8)
#    (1, 23)
#    (2, 45)
#    (3, 12)
#    (4, 78)

Result:

#    [(0, 8), (1, 23), (2, 45), (3, 12), (4, 78)]

Result:

#    0: 8
#    1: 23
#    2: 45
#    3: 12
#    4: 78

Result:

#    0  8
#    1  23
#    2  45
#    3  12
#    4  78

Tested on Python 3.12

 
Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
 
Result:

#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
 
Result:

#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
 
Result:

#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78
 
Result:

#    ('Index:', 0, 'Value:', 8)
#    ('Index:', 1, 'Value:', 23)
#    ('Index:', 2, 'Value:', 45)
#    ('Index:', 3, 'Value:', 12)
#    ('Index:', 4, 'Value:', 78)
 
Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
 
Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
 
Result:

#    (0, 8)
#    (1, 23)
#    (2, 45)
#    (3, 12)
#    (4, 78)
 
Result:

#    [(0, 8), (1, 23), (2, 45), (3, 12), (4, 78)]
 
Result:

#    0: 8
#    1: 23
#    2: 45
#    3: 12
#    4: 78
 
Result:

#    0  8
#    1  23
#    2  45
#    3  12
#    4  78
added 61 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
items = [8, 23, 45, 12, 78]
indices = [0,[]

for 1,index 2,in 3,range(len(items)):
 4]   indices.append(index)

for item, index in zip(items, indices):
    print("{}: {}".format(index, item))
items = [8, 23, 45, 12, 78]
indices = [0, 1, 2, 3, 4]

for item, index in zip(items, indices):
    print("{}: {}".format(index, item))
items = [8, 23, 45, 12, 78]
indices = []

for index in range(len(items)):
    indices.append(index)

for item, index in zip(items, indices):
    print("{}: {}".format(index, item))
added 32 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 25 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 47 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 30 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 5 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 485 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 324 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 4 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 263 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 516 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 278 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 333 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 381 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 430 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s>]. Removed unnecessary formatting.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
deleted 82 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
Commonmark migration
Source Link
Loading
added 256 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
added 302 characters in body
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading
Source Link
Andy Jazz
  • 55.5k
  • 18
  • 152
  • 240
Loading