search

Home  >  Q&A  >  body text

How to print key value in python for in

a=[1,0,1,0]
for i in a:
    print i

This can print 1, 0, 1, 0. I want to print his array key value 0,1,2,3. How to do it

漂亮男人漂亮男人2779 days ago851

reply all(3)I'll reply

  • typecho

    typecho2017-06-12 09:24:57

    a=[1,0,1,0]
    for i, v in enumerate(a):
        print i, v

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-12 09:24:57

    a=[1,0,1,0]
    for index,value in enumerate(a):
        print index

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-12 09:24:57

    Line number output (line number starts from 1)

    my_list = ['a', 'b', 'c']
    for idx, val in enumerate(my_list, 1):
         print(idx, val)

    reply
    0
  • Cancelreply