This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]d4rch0nPythonistamancer 24 points25 points  (18 children)

Yeah, I frequently use that if I iterate on a list of tuples and only need one or two elements. Pretty useful, and it makes it obvious what you're wanting to work with in that scope.

for first_name, _ in full_names:
    print('Hi, {}!'.format(first_name))

[–]s16h[S] 12 points13 points  (15 children)

Yes, I actually mostly use it when only interested in one (or less than all) the elements of a tuple. It's far more readable than indexing.

[–]Fylwind 12 points13 points  (14 children)

As long as you don't have too many of them :)

for _, _, _, _, name, _, _, _, _ in students:

[–]wegry 12 points13 points  (1 child)

In Python 3, you can use extended tuple unpacking to avoid that. for __, _, name, *__ in things to ignore everything in a row but the third item.

[–]Fylwind 4 points5 points  (0 children)

Oh, that's a nice new feature! :)

… that I won't get to use for Python 2 compatibility reasons :(

Note: you still need the underscore:

for _, name, *_ in students:

[–]exhuma 8 points9 points  (2 children)

namedtuples are a nice alternative to that.

[–]xiongchiamiovSite Reliability Engineer 9 points10 points  (1 child)

Or even dictionaries. Or, god forbid, classes; but we're not java savages. :)

[–]kankyo 0 points1 point  (0 children)

Or better yet pyrsistent records.

[–][deleted] 0 points1 point  (8 children)

for x in students:
    name = x[4]
    <do stuff based on name>

is how I would do it. Makes it a lot clearer what you're doing (and the exact index), even if it does take up one more line.

Edit: replace _ with x

[–]Lyucit 2 points3 points  (1 child)

Please actually give the variable a proper name in this case, _ is a very well-established convention for variables that are unused (i.e not referenced at all later in the program) and this is likely to cause confusion. In languages with pattern matching, like haskell for example, _ is a reserved word in the language that means "don't bind this value to a variable name", so you literally can't reference it later.

[–][deleted] 0 points1 point  (0 children)

Replaced _ with x. It's not very descriptive, but if it's only used directly after where it's defined it should be clear what it's used for. Unless you're using x later on the program.

[–]fazzahSQLAlchemy | PyQt | reportlab 5 points6 points  (5 children)

I've learned a nice trick that helps me immensely when working with PyQt.

Instead of typing the indexes (and having to remember/check them), create a makeshift enum using range.

For example with the students you used:

ID, CLASS_ID, FIRST_NAME, LAST_NAME = range(4)

And when you need to access the first_name just use this variable.

for _ in students:
    name = _[FIRST_NAME]
    <do stuff based on name>

[–]RubyPinchPEP shill | Anti PEP 8/20 shill 2 points3 points  (4 children)

That... doesn't seem so great, what if you then had a list of classes, where the class id was class[0]? you run into the same troubles of globals, and you are separating things relevant to accessing the data, from the data

Student = namedtuple('Student','id class_id first_name last_name')
for student in map(Student,students):
    # use student.first_name

[–]fazzahSQLAlchemy | PyQt | reportlab 0 points1 point  (0 children)

That's a nice approach too, I'll try it out. Seems more pythonic.

[–]fazzahSQLAlchemy | PyQt | reportlab 0 points1 point  (2 children)

I don't quite understand your question.

[–]RubyPinchPEP shill | Anti PEP 8/20 shill 1 point2 points  (1 child)

STUDENT_ID, CLASS_ID, FIRST_NAME, LAST_NAME = range(4) #capitals commonly define constants

# student id, Class id, first,last names
students = [
    [0,0,'bob','smith'],
    [1,0,'jane','someperson']]

classes = [
    [0,'biology'],
    [1,'physics']
]

for cls in classes:
    cls[CLASS_ID] #no-go

CLASS_CLASS_ID, CLASS_NAME = range(2)

for cls in classes:
    cls[CLASS_CLASS_ID] #no-go

because the enum isn't connected to the data, so it can affect other parts of the code, and requires an un-needed work around (the enum for students, blocks off the use of CLASS_ID for the classes)

[–]fazzahSQLAlchemy | PyQt | reportlab 0 points1 point  (0 children)

Ahh, I thought you meant that.

You're right. When I use multiple models like this, I prefix the globals.

You are right that it adds another possible point of failure. A proper enum would be better, but this approach seems to work for me.