in Technology by
How to Accessing the index in 'for' loops in Python?

1 Answer

0 votes
by

Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic.

The better option is to use the built-in function enumerate(), available in both Python 2 and 3:

for idx, val in enumerate(ints):
    print(idx, val)

Related questions

0 votes
    How can someone access the index itself for a list like the following : intg = [8, 23, 45, 12, 78, 90] When ... 1 to 6 in this case? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    What does stack overflow' refer to? a. accessing item from an undefined stack b. index out of bounds exception ... from an empty stack Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    What does stack overflow' refer to? a. accessing item from an undefined stack b. index out of bounds exception ... from an empty stack Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    I am a beginner at Django and I am attempting to create a custom table using a html template called ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
+1 vote
    How to find the index of an item in a list in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key ... Python? Or is it simply a variable? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
0 votes
    the following is my question: data = ['a1_1 00000001\n', 'a1_2 00000002\n', 'a1_3 00000003\n', ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
...