I talked a lot about lists ... that in python do the work that in other languages the arrays do.
There is this nice zip method that can make the use of the list even nicer.
Le's say we want to use two list:
people = ["John","Jim","Jack"]
age = [30,22,35]
and we want to use a for loop to make a sentence with the names and the relative age.
Now, we could have make the same thing with a dictonary:
So, it's a matter of taste or depends on the way we store the data that we want to represent. Anyway, it's pretty easy.
There is this nice zip method that can make the use of the list even nicer.
Le's say we want to use two list:
people = ["John","Jim","Jack"]
age = [30,22,35]
and we want to use a for loop to make a sentence with the names and the relative age.
people = ["John","Jim","Jack"]
age = [30,22,35]
for p,a in zip(people,age):
print(p,"is",a)
Now, we could have make the same thing with a dictonary:
people = {"john":30,"Jim":"22","Jack":35}
for p,a in people.items():
print(p,"is",a)
So, it's a matter of taste or depends on the way we store the data that we want to represent. Anyway, it's pretty easy.
Comments
Post a Comment