Home>

Please help me make a cycle that will be from the list of fruits

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
  • Answer # 1

    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
    
  • Answer # 2

    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
    
  • Answer # 3
    fruits= ["apple", 'orange', 'pineapple']
    for number, fruit in enumerate (fruits, 1):
      print (number, fruit)
    # 1 apple
    # 2 orange
    # 3 pineapple
    
  • Answer # 4
    fruits= ["apple", 'orange', 'pineapple']
    for number, fruit in enumerate (fruits, 1):
      print (number, fruit)
    # 1 apple
    # 2 orange
    # 3 pineapple
    
  • Answer # 5
    fruits= ["apple", 'orange', 'pineapple']
    for i in range (0, len (fruits)):
        print (i + 1, fruits [i], sep= '.')
    
  • Answer # 6
    fruits= ["apple", 'orange', 'pineapple']
    for i in range (0, len (fruits)):
        print (i + 1, fruits [i], sep= '.')
    
  • Answer # 7

    Even a very primitive question, index by index.

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

    Even a very primitive question, index by index.

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

    there are several ways:

    viaenumerate, 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]}")
    
  • Answer # 10

    there are several ways:

    viaenumerate, 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]}")
    
  • Answer # 11
    # 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)
    
  • Answer # 12
    # 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)