I am having a problem in my code first see the code I have a list
numbers = [3, 10, 12 ,14, 15, 17, 20]
I want to print all the numbers in the list but I want to have the number of the element before the element so my output should be
1 3
2 10
3 12
and so on I have tried this
for m in range(1 , len(numbers) + 1)
print(m , end = '')
for i in numbers:
print(numbers)
How can I acomplish this
The print statement accepts multiple arguments and prints them with a space in between.
print("hi", "there")
-> "hi there"
So you want:
for i in range(len(numbers)):
print(i, numbers[i])
Note, Python indexes from 0, not 1.
You can use enumerate
, which will give you the index first. (and add 1 if you want the location).
numbers = [3, 10, 12 ,14, 15, 17, 20]
for idx,m in enumerate(numbers):
print(idx+1,' ',m)
Output:
1 3
2 10
3 12
I think you are beginner so here is simple solution
numbers = [3, 10, 12 ,14, 15, 17, 20]
count =0
for i in numbers:
count+=1
print(str(count)+ " " + str(i))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.