0

Please, this might be a very easy question for the python guys out there but for a beginner like me, I am finding it a bit though. I have an array with random, unsorted numbers..and I am looping through it and each time, I want it to print something like this :

i:number in the array..
0:3
1:5
2:6
3:2

So, first it prints the index of i followed by a : character and next it prints the number in that ith index

Here is my code but I don't know how to display this..

for i in range(len(numberInput)):
     print numberInput[i]

Any idea will be appreciated. Thanks

1
  • By the way, you should have a look at the PEP8 conventions: python.org/dev/peps/pep-0008 It won't change the way your code works, but it will be more Pythonesque Commented Jul 2, 2013 at 11:38

1 Answer 1

2

Use enumerate and string formatting:

>>> lis = [3,5,6,2]
>>> for ind, item in enumerate(lis):
...     print "{}:{}".format(ind, item)
...     
0:3
1:5
2:6
3:2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.