Python Forum
Best form of iterating over a lsit of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best form of iterating over a lsit of lists
#1
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)

 
Reply
#2
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
Reply
#3
If you just want to print them, why waste time unpacking them?

for pet in pets:
   print('Name: {}, species: {}, weight: {}'.format(*pet))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
Reply
#5
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
Reply
#6
@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.
:)
Reply
#7
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
Reply
#8
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.
Reply
#9
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Iterating through lists with different letter cases fatherted99 2 1,890 Jun-07-2020, 01:22 PM
Last Post: deanhystad
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,296 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Iterating over two lists in python dgm 2 3,013 Jul-24-2018, 08:31 PM
Last Post: dgm
  iterating over N lists in parallel Skaperen 6 5,192 Mar-24-2017, 06:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020