#πŸ”’ Iterate over a list of tuples while keeping an index

53 messages Β· Page 1 of 1 (latest)

bright fjord
#

I have a list of tuples (of two length), and would like to iterate over both value and the index of the tuple in the list.
I have come up with a work around but I'm asking for is there a way to concentrate the defining of those variables into one line of for loop definition?
(or generally make the code look nicer)

example of desired output:

some_list = [(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]
for index, x, y in some_list:
  ...

what I used instead:

some_list = [(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]
for index, xy in enumerate(some_list):
  for x,y in xy:
    ...
elfin geyserBOT
#

@bright fjord

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

full scaffold
#

Yes.

#

Let me just read over you question some more to make sure I understand it correctly.

#

Okay, so let's take a step back to unpacking and how you can wrangle it.

bright fjord
#

ok

full scaffold
#

!e py a, b, c = 1, 2, 3 print(a) print(b) print(c)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 1
002 | 2
003 | 3
full scaffold
#

Straightforward enough.

bright fjord
#

Yes I understand this.

full scaffold
#

!e py a, (b, c) = (1, (2, 3)) print(a) print(b) print(c)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 1
002 | 2
003 | 3
full scaffold
#

You can also use a structure of variables to match the structure of the thing being unpacked.

#

So in a for loop situation...

bright fjord
#

so instead of

some_list = [(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]
for index, x, y in some_list:
  ...
#

i would have

#
some_list = [(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)]
for index, (x, y) in some_list:
  ...
full scaffold
#

!e py data = [(1, (2, 3)), (4, (5, 6)), (7, (8, 9))] for a, (b, c) in data: print(a, b, c)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 1 2 3
002 | 4 5 6
003 | 7 8 9
full scaffold
#

Or, more specifically...

#

!e py data = [(1, 2), (3, 4), (5, 6)] for indexab in enumerate(data): print(indexab)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | (0, (1, 2))
002 | (1, (3, 4))
003 | (2, (5, 6))
full scaffold
#

!e py data = [(1, 2), (3, 4), (5, 6)] for index, (a, b) in enumerate(data): print(index, a, b)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0 1 2
002 | 1 3 4
003 | 2 5 6
full scaffold
#

The first column being the indexes

#

The second and third being the a and b values

bright fjord
#

does this change if the items in the list are not tuples instead lists of strings?

full scaffold
#

It works on iterability.

#

You'll get tuples out of enumerate

#

once you separate out the index, you're left with the element of the thing you're enumerating over

#

Which you can unpack how you want

#

Providing it's iterable

#

So if it's a list of strings, that'll work, too.

#

!e py data = [['apple', 'pear', 'orange'], ['banana', 'peach', 'lychee'], ['tomato', 'durian', 'lemon']] for i, (a, b, c) in enumerate(data): print(i, a, b, c)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0 apple pear orange
002 | 1 banana peach lychee
003 | 2 tomato durian lemon
full scaffold
#

There's a few other ways of unpacking things, too, using starred forms.

#

!e py a, b, *c = 'abcdefg' print(a) print(b) print(c)

elfin geyserBOT
#

@full scaffold :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | a
002 | b
003 | ['c', 'd', 'e', 'f', 'g']
full scaffold
#

Which can be applied in the for loops, if you need.

bright fjord
#

Ok. So this problem originated from me doing two .split() on a file, resulting in a lists of list of strings.
Was there a easier ways to unpack the data?

#

the data was in the format:

#

data, data, data; data, data;data

#

where the sections are seperated by ';' and individual value are seperated by ','

#

the intention was to check each data value to some range whilst still keeping the index for the outer-most list as the result

#

(where the outer most list is separated by new lines in the txt)

full scaffold
#

Using str.split seems like a reasonable approach, provided there aren't any wriggly exceptions to rules, like escape sequences where you might have a literal ; in the data but not want it to act as a separator.

bright fjord
#

the data was nice enough to be in the format i described

#

does the csv module help me here?

full scaffold
#

It might, but that might just be by accident.

bright fjord
#

I think I'm satisified with this. Thank you for teaching me!

#

!close

elfin geyserBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.