I want to do a foreach of two lists but that in each line it tells me:
Tengo 5 Naranjas
Tengo 6 Peras
Tengo 2 Platanos
I can't find the way and it must be pretty silly to do it but I'm starting with python.
items = ['naranjas', 'peras', 'platanos']
nums = [5, 6, 2]
# without indexes
for item in items:
print("Tengo" + str(nums) + str(items))
There are several ways, the most practical since both lists have the same number of elements, is to "join" both lists through
zip()
which a tuple iterator returns where the first element is the fruit and the second the quantity.The other possibility would be to use an index in some way, for example the one that returns a
enumerate()
:What you should do is traverse the index as follows:
I hope it helps you.
You could use the map function and a lambda function, perhaps it is more efficient since it only writes to the console once.
I show you an example in which the logic that a method is used a little more is to reinforce your sense of programming, you must learn to use the arrays and their individual calls and how the sequences work.