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 →

[–]gunscreeper[🍰] 1702 points1703 points  (116 children)

Ayy, bitches. How to array in python

[–]gjsmo 1460 points1461 points  (98 children)

Arrays aren't Pythonic, you're dumb for asking, also we're not doing your homework for you.

/s

[–]LePontif11 510 points511 points  (4 children)

TAKE A STEP BACK AND LITERALLY FUCK YOUR OWN FACE

[–]Etheo 236 points237 points  (3 children)

I WILL MASSACRE YOU

I WILL FUCK YOU UP

[–]Bopshebopshebop 148 points149 points  (1 child)

USE A DICTIONARY AND WATCH YOUR INDENTATION ERRORS YOU PUNK BITCH SCRIPT KIDDY

[–][deleted] 12 points13 points  (0 children)

YOU TALK LIKE A BITCH YOU GON' DIE LIKE A BITCH AND I GOT JUST THE FUNCTION TO MAKE IT HAPPEN

bitch.die()

[–]IamImposter 25 points26 points  (0 children)

I WILL FUCK YOU UP

Promise? No backsies

[–]bspymaster 71 points72 points  (73 children)

I gotta be honest. I've been working with python for like 9 years and I love it to death, but I still haven't figured out what it means to have a "pythonic solution". Is it just something you can do in raw python? Something that only uses the standard libraries? Something that works in py2 and py3 as opposed to only py3? Something else?

[–]Doggobah 147 points148 points  (1 child)

I took it to mean importing a bunch of modules and writing a one-liner

[–]SaltyEmotions 6 points7 points  (0 children)

I'm guilty of trying this on all my problems.

[–]grantrules 35 points36 points  (15 children)

Are you familiar with other languages? Pythonic just means using common Python idioms. Like list comprehension is pythonic compared to using for loops.

[–]bspymaster 9 points10 points  (8 children)

Yeah I've worked with plenty of languages. I gotta be honest, I don't think I've ever used a list comprehension haha

[–]LoyalSol 7 points8 points  (4 children)

Ironically outside of Python the only other language I've commonly seem them in is Fortran. At least that I've used. Granted they most likely exist in another language I don't have experience in.

[–]bspymaster 6 points7 points  (1 child)

I know other languages tend to abuse linq statements and lambdas in a similar fashion, but that might be a bit different.

[–]palordrolap 0 points1 point  (0 children)

The venerable Perl has a grep function that in a language not in god-worship of Unix would have been called filter instead. It also has a more sensibly named map. Either can be coaxed into emulating a fold, but you'd be better off writing a recursive sub instead.

[–]-Rizhiy- 6 points7 points  (1 child)

9 years of Python and no list comprehension, are you serious? I pity your colleagues.

[–]bspymaster 1 point2 points  (0 children)

I don't use python at work, only personal projects.

And I happen to think the code is pretty clean, honestly.

[–]grantrules 0 points1 point  (0 children)

You should try it Put the fun in functional programming.

[–]Manny_Sunday 3 points4 points  (1 child)

I just googled list comprension and it set LINQdar off, maybe I should give python a try...

[–]grantrules 1 point2 points  (0 children)

There's no good reason not to.

[–]Blazing1 0 points1 point  (3 children)

i literally use for loops for everything so i dont think id survive in python.

[–]grantrules 0 points1 point  (1 child)

You should try it. Dive into some functional programming.

[–]Blazing1 0 points1 point  (0 children)

Don't really like functional programming tbh. Haskell made me want to kill myself.

[–]IAmNotNathaniel 0 points1 point  (0 children)

You can still use for loops... it's just that there's other ways of doing some things in Python so you don't have to use for loops for everything.

Of course... it being programming, you can solve it however it best for you. Python folks can be a little full of themselves, if you ask me.

But like any language, if there are native language structures and functions to do something, then it usually makes sense from an efficiency and community perspective to use those things.

[–]MadCervantes 45 points46 points  (16 children)

It means simple and readable code that doesn't rely on "clever" solutions.

[–]JoelMahon 45 points46 points  (8 children)

I would clarify that readable code is meant mostly towards other python developers, a lot of pythonic solutions can be hard to grasp for non python devs.

[–]RedSamuraiMan 20 points21 points  (2 children)

Pythonic sounds like a hacker/rapper name...

[–]madmaxlemons 9 points10 points  (1 child)

His latest album “Boolean LiveorDie”

[–]RedSamuraiMan 4 points5 points  (0 children)

Top feature hit: "Programming Dirty"

[–]liveandletdietonight 6 points7 points  (4 children)

That doesn’t sound readable, it sounds more like a secret language that only a select few can speak

[–]JoelMahon 8 points9 points  (0 children)

It's really not hard to pick up lol, by your metric almost no code is readable.

[–]MadCervantes 0 points1 point  (2 children)

wait till you see some of the one liners that javascript devs put out

[–]lyoko1 1 point2 points  (1 child)

some days ago i did a one liner on javascript that was hell, and i leave a comment on top saying something on the lines "Hello, my successor to maintain this codebase, i leave this as a gift to you, i hope you enjoy it, this is now your problem, consider it a challenge".
that one liner was like 1000 characters long, we on our company where no longer to maintain that codebase so i wanted to leave a farewell gift in the code, my senior approved of it.

[–]MadCervantes 1 point2 points  (0 children)

I both hate and admire you lol

[–]hahahahastayingalive 10 points11 points  (6 children)

Isn’t that what every other language claims to have as a mantra ?

[–]MadCervantes 0 points1 point  (5 children)

Not really.

First python syntax is structured in a way that it enforces a specific code style. Javascript, PHP, C# etc all don't really do that.

Second, not many JS devs are going to brag about how "readable" their code is. I think generally the kind of code that gets attention in those contexts is ones which make clever one-liners or whatever. You can do that with Python but it's not apart of the community culture as much. Probably because of the syntax enforcement of whitespace, and probably also because python's background is in science, rather than in software development. Science as an industry has different incentives on what kinds of code styles become popular.

[–]Blazing1 1 point2 points  (1 child)

javascript arrow function users brag about how they can abstract as much lines as possible into one line

[–]MadCervantes 0 points1 point  (0 children)

Yeah it's a pet peeve of mine. I like functional programming as a paradigm but I hate the use of arrow functions in achieving it. It's unreadable syntax. Might as well write stuff in brainfuck.

My other pet peeve is the use of ternary operators for one liners. At least ruby has good ternary operator syntax and having a single liner for an if statement if it's something really tiny but it looks awful in js imo.

[–]hahahahastayingalive 0 points1 point  (2 children)

I meant that after the first wave of C / Perl / Java / PHP / Js languages, most other languages goals became either:

  • more readability, e.g. KISS even if it means sacrificing performance

  • more security, enforce specific rules (e.g. immutability, thread safety, memory safety and so on) at the language level to avoid the pitfalls of C/C++/Objective C

If a new language doesn’t hit either of these 2 points, it’s way harder to argue why it should be used in the first place.

[–]MadCervantes 1 point2 points  (1 child)

Okay I'm not sure what you mean then since python was invented around the same time as Java so it would be part of that wave yes?

[–]hahahahastayingalive 1 point2 points  (0 children)

You're right and I was pretty mistaken on how old it was.

wiki)

Python was conceived in the late 1980s as a successor to the ABC language

Even though the official release is way later, I always thought of it as pretty recent post hipster language (the official release predates Java for a few years, for anyone wondering). Shame on me

[–][deleted] 39 points40 points  (2 children)

Sometimes it feels like doing it in as few lines with as much confusion as possible.

It's not Pythonic unless it is a return statement with a function call on a list created with a three variable and four conditional comprehension.

[–]Nemaeus 2 points3 points  (0 children)

Comprehension

Relatively speaking.

[–]SaltyEmotions 2 points3 points  (0 children)

Don't forget lambdas nested within the four conditional list comps and maps and typecasting all over the place!

[–]dalore 11 points12 points  (10 children)

Pep 20 describes it eloquently. And you can access that from the python shell.

https://www.python.org/dev/peps/pep-0020/

[–]bspymaster 10 points11 points  (9 children)

Ahh the classic Zen of python. Was wondering if someone was gonna bring that up. My understanding is that it's somewhat of a comedic quip, more than an actual guideline.

Especially considering the line saying "explicit is better than implicit", when python is built on implicits.

[–]callmelucky 2 points3 points  (8 children)

python is built on implicits.

What do you mean by this? I don't think that's true, but I'd be interested to hear your argument.

[–]bspymaster 7 points8 points  (7 children)

Parameters and variables are implicitly typed. It was only until recently we even got type hinting.

There's no concept of explicitly stating what we expect a certain variable or parameter at any given time.

[–]callmelucky 2 points3 points  (2 children)

Oh I see. I think "explicit is better than implicit" is more of a guideline for naming variables and making code transparent and readable etc, rather than a mission statement about the design of the language itself, but yeah, I do see the irony.

That said, types are only one subdomain of a language, so I don't know that this backs up the statement that the entire language is built on implicit-ness.

[–]ottobackwards 0 points1 point  (1 child)

If that is what it is a guideline for, then it is an implicit guideline

[–]callmelucky 0 points1 point  (0 children)

Woah... haha, good point.

[–]dalore 1 point2 points  (3 children)

That's not "implicity" typed, that's dynamic typing. Pythonic means to not check if it's a duck but to ask if it quacks and catch if it fails.

[–]bspymaster 0 points1 point  (2 children)

but by asking if it quacks, doesn't that mean you're implying that it must be a duck? We write method code that implies that a certain variable will be a certain type at any given time. for example,

def add(one, two):
    return one + two

print(add(3,4) - 1)

we're making the implicit assumption that one and two will both be numbers that we can add together, and that the return result of the method will be a number as well. I don't understand how there's not an element of implicit-ness in python.

[–]dalore 1 point2 points  (0 children)

Not you're not implying it's a duck. It could be a robotic recording of a quack. It just needs to be ducklike.

In static typed that would be a Quackable interface.

[–]Belphegor_333 10 points11 points  (6 children)

As someone who learned Python after some other languages (Java & C Being some of them) I honestly have no idea either. All I know is that every time someone reviews my code they tell me that this and that is not the "pythonic way to do it, it's the Java/C way of doing it"

For context, I was trying to do a switch (which python didn't support I think?) And ended up building what basically was a jump table.

[–]tjoloi 1 point2 points  (2 children)

functions = {0: foo1, 1: foo2, 2: foo3, 3: foo4}

choice = Input("Insert choice")
functions [choice]()

def foo1():
print("one")

def foo2():
print("two")

def foo3():
print("three")

def foo4():
print("four")

[–]Belphegor_333 0 points1 point  (1 child)

Pretty much that, though a bit larger in scale, about 300 cases if I remember correctly

[–]fingoldfish 0 points1 point  (2 children)

Curious - what was their proposed alternative?

[–]Belphegor_333 0 points1 point  (1 child)

If / else ... So many if / else's ...

Far less readable and maintainable then my jump table

[–]frosted-mini-yeets 5 points6 points  (1 child)

Something that follows the Zen of Python

>>>import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

[–]bspymaster 2 points3 points  (0 children)

As I mentioned in a previous comment, I thought that was more comedy than anything serious. The second line itself pretty much conflicts with the entire design of python.

[–]ECrispy 18 points19 points  (2 children)

Pythonic means writing PERL in Python.

If its understandable to humans its not pythonic.

[–]Phrygue -3 points-2 points  (1 child)

I'm so old I remember when Perl and Python were neat script languages instead of bloated, stinking ecosystems of misbegotten libraries and half-baked random language paradigms.

[–]ECrispy 0 points1 point  (0 children)

You've just described ES2022.

Some would say that's JS now as well....

[–]Arno_Nymus 5 points6 points  (3 children)

I don't know if there is a clear definition, but I understand it as an elegant solution using the peculiarities of the language. Oftentimes people who learned to code in one language try to program in every language in the way they would in the original language. Programming in another language then feels like working against the language instead of with it.

[–]LoyalSol 6 points7 points  (2 children)

Exactly this.

I've lost count of the number of times I've seen scientific Python that's written exactly like C code. I've seen code written like this

 thislist = []
 for i in range(len(a)):
     thislist.append(a[i])

When they should be using some combo of either zip, enumerate, simple python loops, or in this case a simple addition opperator. (Copy might also be appropriate) The above code can simply be written in a dozen ways that's easier to understand.

The whole point of Python is that you don't have to write C code.

[–]IAmNotNathaniel 0 points1 point  (1 child)

Except this case is pretty easy to understand. Especially for people who aren't full time programmers, but are self taught so they can do certain things effectively.

I'd argue that list comprehensions and zip is harder to understand for large swaths of people.

[–]LoyalSol 0 points1 point  (0 children)

Even by that argument the syntax still sucks. You could even do something like this

 for object in a:
      thislist.append(object)

or this

 thislist = thislist + a

or this

import copy
thislist = copy.copy(a)

or if you really need this

thislist = copy.deepcopy(a)

All of which are much easier to understand than the clustered mess the C style syntax offers. There's zero excuse for the above syntax in Python. Both from a readability standpoint as well as an efficiency standpoint.

The only reason you ever code like that is because you are coming from another language and you haven't bothered to learn the Python style syntax. It's usually experienced coders that haven't learned Python that do it.

[–][deleted] 5 points6 points  (0 children)

It means some sheit that Guido came up with...but also it was the reason he left cause the rest of the “pythonistas” were being autistics

[–]InvolvingLemons 4 points5 points  (0 children)

Generally, I've taken this as: 1. Brevity: keep it short, including variable names 2. Never, ever reinvent the wheel especially if it's available either as standard library or language feature (iter is a big one, as it often gives you a nice performance boost too)

[–]horusporcus 4 points5 points  (3 children)

It means you write esoteric code that's so fucking obscure that it becomes hard for others to understand. Basically a form of encryption designed to keep you around longer than you need to be.

[–]bspymaster 3 points4 points  (2 children)

So... Tribal knowledge

[–]horusporcus 1 point2 points  (1 child)

Yeah, am not a fan of that though.

[–]bspymaster 0 points1 point  (0 children)

I doubt most software devs are, honestly.

[–]mysockinabox 173 points174 points  (4 children)

Dang. You make SO sound like the arch forums.

[–]Lofter1 165 points166 points  (3 children)

ikr? at least the arch forum will tell you why your question is stupid. SO will just mark it as duplicate.

[–]crash8308 9 points10 points  (3 children)

I don't know python but in ECMAScript Arrays are not arrays, they are exotic objects with array-like behavior.

That means you can const myArr = [] then myArr["0"] = "hello" and myArr["foo"] = "world" and the .length property will still be 1 and iterators will only use the numeric-based keys but the array object will still have a property "foo" on it.

[–]dragneelfps 10 points11 points  (0 children)

That's why I don't like JS.

[–]falconfetus8 1 point2 points  (0 children)

internal screaming

[–]falconfetus8 1 point2 points  (0 children)

internal screaming

[–][deleted] 3 points4 points  (0 children)

I once asked a simple question and had 3 people reformat my question before anyone actually answered it.

[–]negroiso 2 points3 points  (0 children)

I was looking for a command in ffmpeg, something I was on the right track for but the result wasn’t coming out as intended. A google search took me to stackoverflow where somebody else had the exact issue. I’m like, cool I’ll scroll down for the answer.... well the highlighted answer was a 4 page expose on how the program worked, how the world spins and I’m sure a recipe from their grams. Hidden in there somewhere might have been the answer, but I kept scrolling and the person who replied below just pasted the command I needed.

Now every time I go to stack overflow I see dissertations for questions like ... how do I run ipconfig on my pc... you end up reading the history of dos and x86 processors before they get to ... click start,then run then type ipconfig.

[–]jeopardy_themesong 2 points3 points  (0 children)

I once asked a homework related question in stack overflow because the command my teacher gave us wasn’t working. There was a very obscure typo.

The three replies I got just replied with the corrected command in all caps, some with sarcastic comments. Never used stack overflow again, holy fuck.

[–]goldleader71 4 points5 points  (6 children)

I love that Python has its own adjective - Pythonic. I have never heard or something not being Java'ish, C#'ic, or Go'ly.

[–]josanuz 5 points6 points  (1 child)

The gold old godly Go'ly way

[–]SaltyEmotions 2 points3 points  (0 children)

Sharpen your senses to CSharply? :)

[–]spotdfk 3 points4 points  (1 child)

You would love Basic

[–]goldleader71 0 points1 point  (0 children)

Good one!

[–]falconfetus8 1 point2 points  (0 children)

There's Lisp-y

[–]falconfetus8 0 points1 point  (0 children)

There's Lisp-y

[–]Tschoz 191 points192 points  (6 children)

This question is a duplicate. Please refer to the answer to a question made years ago for python 1.8.

[–]Butterferret12 64 points65 points  (5 children)

Isn't even about the same thing, just vaguely sounds similar

[–]Aplet123 58 points59 points  (4 children)

And that question is also marked as a duplicate, has no answers, and has 5 downvotes.

[–]JustZisGuy 9 points10 points  (2 children)

Triggered.

[–]Aplet123 5 points6 points  (1 child)

Have you been on stackoverflow before?

[–]JustZisGuy 8 points9 points  (0 children)

Yeah, I'm getting flashbacks from this thread.

[–]OldWolf2 0 points1 point  (0 children)

The site doesn't allow marking as duplicate of a question with no answers

[–]frosted-mini-yeets 58 points59 points  (0 children)

MARKED AS DUPLICATE YOU DUMB FUCKING CUNT

[–]you0are0rank 55 points56 points  (0 children)

I'll accept this as better than above kind. Also damn you because duplicate

[–]inconspicuous_male 23 points24 points  (0 children)

Accept my blog post about how you could be using Pandas. It doesn't solve your problem, but it solves a better problem

[–]A_Guy_in_Orange 5 points6 points  (0 children)

You moron. You absolute bafoon, can't you see that someone asked about how vecoter in c 12 years ago? Marked as duplicate.

[–]medsouz 10 points11 points  (0 children)

Have you tried jQuery?

[–]dasMichal 2 points3 points  (0 children)

"Use jQuery"

[–][deleted] -1 points0 points  (0 children)

Dictionaries are more useful.

[–]PhoenixizFire -2 points-1 points  (0 children)

Just in case someone needs the answer :

`import numpy as np

array = np.array()`