A BETTER Way To Loop Python Lists! #python #coding #programming

preview_player
Показать описание
This quick video shows you how to use enumerate to loop through your Python lists.
Рекомендации по теме
Комментарии
Автор

One-liner, for fun:

[ print(f"{slot}: {item}") for slot, item in enumerate(inventory) ]

justagyraffe
Автор

I like seeing the output so i can understand what you inputted

adamharb
Автор

ever since I found out about the enumerate function, it's made coding much easier.

imma
Автор

Some comments are suggesting string interpolation with str.join and a generator. This is an option, but I think a standard for loop is preferred here. In this case it doesn't really matter, but in most situations a smooth stream of output is preferred over a delay following by an instant output of all data

schlopping
Автор

Im a one-liner type of guy and that’s why python is just my fav language. I started learning otters like C but it’s so tedious to write all that stuff that in python it would take 1-2 lines to do the same thing. But I’m still learning it cus ik that some projects require that extra speed C can give tho

vinicus
Автор

For one-liner what I thought of was:

print('\n'.join(f'{slot}: {item}' for slot, item in enumerate(inventory)))

straizer
Автор

May you all be successful in whatever field you wish to pursue .

nu
Автор

this looks like LUA programming (for slot, item in pairs(inventory)) i love make code simpler

asimpleorbasicRobloxdevfrom
Автор

I wish more languages had an enumerate-like system for for loops

OctagonalSquare
Автор

The first algorithm works in any programming language! While the "better way" only works in python. I personally recommend the first way!!

ronaldmigahil
Автор

How fast is it? Or it just save one line of code?

GregBreak
Автор

Dude, im a huge fan of what you do, but be careful about choice of words. "Can you think of a better way to code this" makes me think that just because we're executing fewer lines to accomplish the same task, we're writing "better" code.

Maybe in THIS case we are, but in python there's a lot of times where the added readability of a one-liner comes with a significant performance cost.

The reason I think that it's important to make this distinction is that, as an amateur programmer trying like many others to break into tech, I've failed dozens of coding exams and interviews for this exact line of thinking.

We are often times, especially if we admit to preferring python as your language (its almost as if interviewers roast you even harder if you want to use python), given constraints in these situations where we are required to solve a problem "in place", meaning that we are NOT allowed to make any copies of the input data.

That constraint is what kills alot of us quick, because it takes away the ability to use these tricks that are discussed so succinctly here.

WhNeedsFrends
Автор

Would love to see how that performs for a list with 1 million elements

RoxasYami
Автор

No array interpolation? Just strring.join your enumerates in py?

akzual
Автор

What the asterisk, at the end of the video, does?

adiveler
Автор

What is tha meaning for 'f' bro inside print

sathya
Автор

for i, v in pairs (inventory) print inventory end I don't know I don't use c#

andykwan-dm
Автор

You don't need the range(len(inventory)) at all. You can iterate through a list just by declaring as your object of your loop. Cleaner code would be
"
for slot in inventory
print(inventory[slot])
"

timothypasket
Автор

Thanks for video but enumerate() is still not clear to me.

lukaschumchal
Автор

Yeah but the other option is way more readable, one variable created in the scope of a for is not going to do anything, especially when you don’t know how many objects are created with those instructions you are using.

Tmplar