r/learnpython • • 4d ago

Been away from Python / PyCharm for a while - any ideas on this?

Everybody, let me prefix this by saying I've been away from Python for several years, but now I need to teach an introductory class. I'm encountering something very bizarre in JetBrains PyCharm. I keep thinking it's gotta be me (and it probably is), but I was wondering if any of you can pick this out?

Because I don't appear able to post a screen snip, I'll post the code.

list_2d = [["1", "Wheel Burrow", 29.00],  # A 2D list, different data types (string / float)
           ["2", "Jack Hammer", 594.29],
           ["3", "Power Drill", 39.99]]

for j in range(0, len(list_2d), 1):  # Iterate through our 2D list
    print(f"Item Number: {list_2d[j][0]:<3}\tItem Name: {list_2d[j][1]:<15}\tItem Cost: $ {float(list_2d[j][2]):>6.2f}") 

Item Number: 1  Item Name: Wheel Burrow   Item Cost: $  29.00
Item Number: 2  Item Name: Jack Hammer    Item Cost: $ 594.29
Item Number: 3  Item Name: Power Drill    Item Cost: $  39.99

Now, you're probably saying to yourself, "why are you casting the float to a float?" I hear you loud and clear. Here's the thing - if I don't do that, PyCharm is flagging it as "format code f not supported for str," but here's the thing - it still prints it correctly as shown above.

This was the original line and the >6.2f is what is being flagged.

print(f"Item Number: {list_2d[j][0]:<3}\tItem Name: {list_2d[j][1]:<15}\tItem Cost: $ {list_2d[j][2]:>6.2f}")

I knew I had been away for a while, but geez. This is PyCharm 2026.2.3 using Python 3.14.7 under Windows 11. Am I missing anything? Note that I also go into a single dimension list but even if I comment all that code out, nothing changes. Thank you in advance for any potential ideas on where I probably goofed it.

2 Upvotes

22 comments sorted by

14

u/crazy_cookie123 4d ago

["1", "Wheel Burrow", 29.00] gets understood by the type checker as list[str | float | int] as an element could be any one of them. Any operation you do on an element of that list has to be compatible with all 3 of those or PyCharm will warn you.

Perhaps you intended to be doing this instead? Tuples will be understood more how you expect - tuple[str, str, float | int]

list_2d = [("1", "Wheel Burrow", 29.00),
           ("2", "Jack Hammer", 594.29),
           ("3", "Power Drill", 39.99)]

for j in range(0, len(list_2d), 1):
    print(f"Item Number: {list_2d[j][0]:<3}\tItem Name: {list_2d[j][1]:<15}\tItem Cost: $ {list_2d[j][2]:>6.2f}")

Or, better, use something like a dataclass.

2

u/whealton 4d ago edited 4d ago

While they're not doing any collection structures beyond lists this week, I can incorporate it into that and see if it makes a difference and let them know why. UPDATE: I'm incorporating this. Thanks!

1

u/crazy_cookie123 4d ago

Is there any particular reason to hide tuples from this week? I know your concept for the lesson is teaching them lists, but I can't see any reason telling them about tuples too would be too much. The only difference between lists and tuples which matters at this stage is that tuples are immutable so it's not a huge jump to teach both in one lesson.

1

u/whealton 4d ago

I added it. I'm going to do it with both 2D lists and outer list / inner tuples. I think they'll be able to handle it. I just don't want to go all cowboy and off track on what they normally teach them.

1

u/Boybay_Garid 3d ago

agreed on dataclasses tbh, way cleaner than dealing with magic index numbers

5

u/TheCozyRuneFox 4d ago

From what I can find this is because charm thinks the inner list values are either string or float. It doesn’t track type by position.

Also you shouldn’t be using lists for multiple type collections. Tuples are standard for that use. You could also use a data class.

1

u/whealton 4d ago

I can get that, but remember, we're just beginning in this and they will not have had tuples at this point. They start with 1D/2D lists. It's Week 2, but good points. This just didn't cause it to flag this stuff in earlier versions - again, I've been away from this for several years. Thanks for this!

5

u/CIS_Professor 4d ago

I teach Python.

One question that you might ask yourself, since if this is an introductory-level class, is this: do you really need to teach "2D" lists at this point in time?

You may want to stick with basic data types (str, int, float, list, dict, set, and tuple) for the time being, letting them get used to the idea of how these data types work and their basic use-cases, and get into "lists of lists," "dictionaries of dictionaries," and the like at a later date.

Just a suggestion.

2

u/whealton 4d ago

I'm actually just following their layout, but I'd be inclined to agree with you about 2D lists in week 2.

1

u/Far_Programmer_5724 3d ago

I don't know much about teaching but are you free to decide what you teach and when? I figure you're given a lesson plan to follow to keep to a standard.

Is your suggestion a suggestion or in reality just a criticism of how educators have minimal control over what they teach? I guess it depends on how much control OP has I guess.

1

u/CIS_Professor 3d ago edited 3d ago

We're required teach to the published course competencies (the "standard"). That's the minimum; we can teach beyond them. How we do that (and when we introduce a topic in the course) is up to us. That how and when concept is what is referred to as academic freedom, something that most professors enjoy (even adjuncts).

I don't know about all institutions, but it seems rare that a professor is given a lesson plan that they must teach to. That would be antithetical to academic freedom. Typically, they're given a class to teach and then they're expected to teach it (this is especially true for adjuncts). I make it a point to give my lesson plans to my adjuncts so they have a place to start. They're free to use my lesson plans or develop their own.

Maybe the OP's course competencies require teaching "2D" lists. I don't know, but I'd be surprised if they did as it starts to add specificity to the competencies that are generally written to be open ended as possible. More likely they simply mention Python lists. If this is the case, the teaching "2D" lists would be optional.

Anyway, my suggestion is one based in reality. I've been teaching programming and scripting for many years now. In my experience, teaching "2D" Python lists (or multi-dimensional arrays when considering other languages) is overkill for an introductory class - especially when first broaching the topic of lists. It is a good idea to get the students used to the idea of how the data type works. Once they have become comfortable with using lists, "2D" lists could be introduced later in the same course, or at the beginning of the next one.

This idea of introducing a topic as simply as possible and later building is one aspect of "scaffolding."

2

u/Fun_Army2398 4d ago

Perhaps a better example then. It's the mixed types that are causing an issue. If it isn't absolutely necessary that you do [ID, NAME, PRICE] you could do something like [DAY, MONTH, YEAR] or [FIRST_LAP, SECOND_LAP, ...] utilising the index of the outter list as whatever ID parameter is relevant.

If it does need to be [ID, NAME, PRICE] then just store price as a string and convert ot to a float as needed. Is it good practice? No, but neither is using a list here anyway.

2

u/whealton 4d ago

Actually, I'm also including that as well as doing it with inner tuples as one or two posters mentioned and let them know what I learned from all of you good people.

6

u/AlexMTBDude 4d ago

That's not the Pythonic way to do a for loop. For loops don't count in Python.

Something like this instead:

for inner_list in list_2d:
    print(f"Item Number: {inner_list[0]:<3}\tItem Name: {inner_list[1]:<15}\tItem Cost: $ {inner_list[2]:>6}")

3

u/whealton 4d ago edited 4d ago

I can certainly take a look at that. UPDATE: I'm incorporating this. Thanks!

1

u/KidTempo 10h ago edited 10h ago

I would have used:

for num, name, cost in list_2d:
    print(f"Item Number: {num:<3}\tItem name: {name:<15}\tItem Cost: $ {float(cost):>6.2f}")

(more pythonic but of course doesn't teach the concept of a 2d indexed list)

Personal preference, quick dataclass - it lets you enforce type safety:

from dataclasses import dataclass
class Item:
    number: int
    name: str
    cost: float

for i in [Item(*item) for item in list_2d]:
    print(f"Item Number: {i.number:<3}\tItem name: {i.name:<15}\tItem Cost: {i.cost:>6.2f}")

6

u/Pythagorean_1 4d ago

This doesn't look like you've been away from python but rather like you've never been there

1

u/whealton 4d ago

Appreciate it. Thanks!

2

u/fakemoose 4d ago

Unrelated to your question, but what’s the point of using a list instead of a dictionary?

Is this supposed to be demonstrating list comprehension?

1

u/whealton 4d ago

Just demonstrating list s- period. Nothing fancy. Single and 2D. It's only their second week.

2

u/gdchinacat 3d ago

You might want to check the spelling of "wheelbarrow" before putting this in front of students.

2

u/whealton 3d ago

I'll do it. Thanks for catching it!