Python Forum

Full Version: Best form of iterating over a lsit of lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Are any of the following ways of iterating over a list of lists better than the otheres (or even different)?


pets = [["Garfield", "cat", 4],
        ["Dog", "dog", 7.5], 
        ["Porkey", "pig", 61],
        ["Stoned", "rock", 0.230] ]


for name, species, weight in pets:
     print(name, species, weight)

for [name, species, weight] in pets:
     print(name, species, weight)

for (name, species, weight) in pets:
     print(name, species, weight)

 
this can be simplified even more with a list comprehension:
pets = [["Garfield", "cat", 4],
        ["Dog", "dog", 7.5],
        ["Porkey", "pig", 61],
        ["Stoned", "rock", 0.230]]

for pet in pets:
    name = pet[0]
    species = pet[1]
    weight = pet[2]
    print('Name: {}, species: {}, weight: {}'.format(name, species, weight))
results:
Output:
Name: Garfield, species: cat, weight: 4 Name: Dog, species: dog, weight: 7.5 Name: Porkey, species: pig, weight: 61 Name: Stoned, species: rock, weight: 0.23
If you just want to print them, why waste time unpacking them?

for pet in pets:
   print('Name: {}, species: {}, weight: {}'.format(*pet))
Thanks for both your replies. They make it clear, I should have explained why I care about the question (I was trying to be concise!). I'm teaching Python (to first year engineering students). I've programmed in many languages but teaching this course is the only Python I do. (FWIW, I've come to like the language quite a lot!) My problem is, I'm not clear about the differences between the three forms of the loop that I've written and I feel that is a gap in my understanding of Python that I'd like to fill in. So, the examples are contrived (as are most examples when you’re teaching) but I'm looking for understanding, not just what works.

These forms look a bit like two lists (with and without the surrounding [ ]), and a tuple. But clearly, they're not as they are begin assigned to. So this seems like a special syntax for that purpose (perl has something similar), but if it is, why are there three forms of it? Are the different in any way? Or is there some more general feature of Python going on here that I don't understand?

TIA

Tony
similar to a 2d array in C, except in C, you'd have to first create a typedef for single element (because of mixed data types)
like:
typedef struct {
   char *name,
   char *species,
   double weight
} pet;

struct pet[4] = ...
Here you don't worry about the mixed data types. Much easier.

My 'C' is rusty, so hope I got it right
@Larz60+ , I think @tonymcgregor is confused about the syntax of the for loops.
He wants to know the differences between following three codes (I also want to know):
for name, species, weight in pets:
for [name, species, weight] in pets:
for (name, species, weight) in pets:
Please let us know the differences.
:)
When you loop for name, species, weight in pets:  is equivalent to name, species, weight = pets[0] loop name, species, weight = pets[1] loop ...ect.
So you are unpacking values to a tuple name, species, weight.
Why is it a tuple?
>>> pets = [["Garfield", "cat", 4],
...         ["Dog", "dog", 7.5],
...         ["Porkey", "pig", 61],
...         ["Stoned", "rock", 0.230] ]

>>> name, species, weight = pets[0]
>>> name, species, weight
('Garfield', 'cat', 4)

>>> # Or to make it clear
>>> 'a', 'b', 'c'
('a', 'b', 'c')

>>> name
'Garfield'
>>> species
'cat'
>>> weight
4
So can use () or [],but it's not needed and in a loop is just confusing.
Outside of loop it can be used to make clear that it's a tuple.
>>> tup  = 1, 'a'
>>> tup
(1, 'a')

>>> tup1 = (1, 'a')
>>> tup1
(1, 'a')

>>> tup == tup1
True
Thanks for your reply.

I get that 'a', 'b', 'c' in is a tuple. But is name, species, weight also a tuple in the following?

name, species, weight = pets[0]
In this case name, species, weight is the left hand side of an assignment. I wouldn't have thought it was a tuple but if it is, it isn't an "ordinary" tuple. You can't, of course, write (2, 3, 4) = something

So I still have the sense I'm missing something.
In [1]: pets = [["Garfield", "cat", 4],["Dog", "dog", 7.5],["Porkey", "pig", 61],["Stoned", "rock", 0.230] ]

In [2]: for pet in pets:
   ...:     name, animal, weight = pet
   ...:     print(name, animal, weight)
   ...:     
Garfield cat 4
Dog dog 7.5
Porkey pig 61
Stoned rock 0.23
Thanks for answering but I think you've misunderstood my question. I'm trying to understand the difference in these:

for name, species, weight in pets:
     print(name, species, weight)
 
for [name, species, weight] in pets:
     print(name, species, weight)
 
for (name, species, weight) in pets:
     print(name, species, weight)
Especially the last two. I know what they do, but is there a difference? Asked another way, does it make a difference if you use () or [] on the left hand side of an assignment? If not in my case, does it ever make a difference if there is a tuple (or other iterable) on the right hand side?
Pages: 1 2