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
#11
eventually you can use named tupe
 
from collections import namedtuple

Pet = namedtuple('Pet', ['name', 'animal', 'weight'])

pets = (Pet('Garfield', 'cat', 4), Pet('Pluto', 'dog', 10))
for mypet in pets:
    print(mypet)
    print('{} is a {} and its weight is {}'.format(*mypet))
    print(mypet.name, mypet.animal, mypet.weight)
Output:
Pet(name='Garfield', animal='cat', weight=4) Garfield is a cat and its weight is 4 Garfield cat 4 Pet(name='Pluto', animal='dog', weight=10) Pluto is a dog and its weight is 10 Pluto dog 10
Reply
#12
Use the first one in a loop,the all will work the same.
for name, species, weight in pets:
    print(name, species, weight)
The name of the this stuff is called tuple assignment and tuple unpacking.
>>> info = ('julia', 'borger', '27')
# The normal way
>>> name, surname, birth_year = info
>>> name
'julia'
>>> surname
'borger'
>>> birth_year
'27'
# Can do this,but not so common
(name, surname, birth_year) = info

# Can do this,whaaat?
[name, surname, birth_year] = info
Reply
#13
There's no real difference. You might be wasting a little time with the latter two, and they look a little odd, but there's no real difference.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
I've done what I should probably have done in the first place; read the spec. I think I basically understand now but htere's one thing still bothering me.

There is a difference between () and [] on the LHS of an assignment but it's quite subtle.
(a) = is the same as a =
[a] = requires an iterable on the RHS (with just one value)
So (a) = 2 is valid but [a] = 2 is not.

I'm guessing this use of parenthesis is to be consistent with the rules for creating tuples and lists. In (1,2) the parentheses don't make this a tuple, the comma does. The parentheses are, actually (redundant) "ordering" parentheses as in (2 + 3). On the LHS they do the same thing.
E.G.
a,(b,) = 1, 2      #invalid
a,(b,) = 1, 2,     #invalid
a,(b,) = 1, (2,)   #OK
a,(b,) = 1, (2)    #invalid
a, b   = 1, 2,     #OK
a, b,  = 1, 2      #OK
But, what I still don't get is why allow [] on the RHS at all. a, requires and iterable on the RHS just like [a]. I.E.:
(a) = 1,  #a is a tuple.
[b] = 2,  #b is an integer
#but so is c in 
c, = 1,
I can't see a need for [] on the LHS so it seems odd to allow them. Especially given the confusion with lists and tuples.
Reply
#15
Maybe [] are allowed on the LHS for slice assignment? some_list[2:5] = range(4)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
You're right, my statement "I can't see a need for [] on the LHS" was to broad. I do see their use in a slice assignment. But still not in assignment to a target list.
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,888 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,293 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Iterating over two lists in python dgm 2 3,010 Jul-24-2018, 08:31 PM
Last Post: dgm
  iterating over N lists in parallel Skaperen 6 5,188 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