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
#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


Messages In This Thread
RE: Best form of iterating over a lsit of lists - by Larz60+ - Sep-19-2017, 02:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,440 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Iterating through lists with different letter cases fatherted99 2 2,616 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 5,380 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Iterating over two lists in python dgm 2 3,878 Jul-24-2018, 08:31 PM
Last Post: dgm
  iterating over N lists in parallel Skaperen 6 6,680 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