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


Messages In This Thread
RE: Best form of iterating over a lsit of lists - by tonymcgregor - Oct-15-2017, 10:37 PM

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