Home>

### Help with a light loop in python

`fruits= ["apple", 'orange', 'pineapple']`print each value and add a sequential number to get

1. apple
2. orange
3. pineapple

Thanks in advance, and sorry for this likely easy task for you.)

print (list (map (lambda x: f '{x [0] + 1}. {x [1]}', enumerate (fruits))))

entithat2021-11-22 02:33:12

You can use the generator to solve the problem in one line. And thus, save memory if the list is very large:

``````fruits= ["apple", 'orange', 'pineapple']
[print (f '{number + 1}. {fruit}') for number, fruit in enumerate (fruits)]
``````

Conclusion:

``````1. apple
2. orange
3.pineapple
``````

You can use the generator to solve the problem in one line. And thus, save memory if the list is very large:

``````fruits= ["apple", 'orange', 'pineapple']
[print (f '{number + 1}. {fruit}') for number, fruit in enumerate (fruits)]
``````

Conclusion:

``````1. apple
2. orange
3.pineapple
``````
``````fruits= ["apple", 'orange', 'pineapple']
for number, fruit in enumerate (fruits, 1):
print (number, fruit)
# 1 apple
# 2 orange
# 3 pineapple
``````
``````fruits= ["apple", 'orange', 'pineapple']
for number, fruit in enumerate (fruits, 1):
print (number, fruit)
# 1 apple
# 2 orange
# 3 pineapple
``````
``````fruits= ["apple", 'orange', 'pineapple']
for i in range (0, len (fruits)):
print (i + 1, fruits [i], sep= '.')
``````
``````fruits= ["apple", 'orange', 'pineapple']
for i in range (0, len (fruits)):
print (i + 1, fruits [i], sep= '.')
``````

Even a very primitive question, index by index.

``````fruits= ["apple", 'orange', 'pineapple']
for i in range (len (fruits)):
print ("" + str (i + 1) + "." + fruits [i])
``````

Even a very primitive question, index by index.

``````fruits= ["apple", 'orange', 'pineapple']
for i in range (len (fruits)):
print ("" + str (i + 1) + "." + fruits [i])
``````

there are several ways:

via`enumerate`, which itself will add sequential indices to the elements

``````fruits= ["apple", 'orange', 'pineapple']
for obj in enumerate (fruits, 1):
print (f "{obj [0]}. {obj [1]}")
``````

or through a normal index pass:

``````for i in range (len (fruits)):
print (f "{i + 1}. {fruits [i]}")
``````

there are several ways:

via`enumerate`, which itself will add sequential indices to the elements

``````fruits= ["apple", 'orange', 'pineapple']
for obj in enumerate (fruits, 1):
print (f "{obj [0]}. {obj [1]}")
``````

or through a normal index pass:

``````for i in range (len (fruits)):
print (f "{i + 1}. {fruits [i]}")
``````
``````# mark
fruits= ['apple', 'orange', 'pineapple']
n= 0
for fruit in fruits: # We iterate over all fruits and add a number
n += 1
print (str (n) + '.' + fruit)
``````
``````# mark