The Python code snippet del a[i] deletes the element at index i from a list a. This code certainly is quite readable, but not so transparent: it’s not clear if the operation is order-preserving and if the time complexity is constant or linear.

🎙️ cmov · 115 points · Posted at 07:50:23 on April 25, 2019 · (Permalink)


Saved comment

🎙️ cmov · 129 points · Posted at 07:53:17 on April 25, 2019 · (Permalink)

Don't you hate it when a "delete element from list at index" function also shuffles the list?

n3f4s · 72 points · Posted at 07:58:57 on April 25, 2019 · (Permalink)

I remember one time when I had a del a[i] in my code and each time the del was executed, all the other element from the list were multiplied by a random factor. It was so annoying.

griever989 · 55 points · Posted at 13:53:53 on April 25, 2019 · (Permalink)

You may have accidentally been using del a[i] instead of del a[lmao]. If you delete the AI from your code then it will only run at 0.01x efficiency.

ponybau5 · 15 points · Posted at 13:52:13 on April 25, 2019 · (Permalink)

Mine ended up playing roulete and deleted whatever it wanted to

JayCroghan · 23 points · Posted at 14:43:02 on April 25, 2019 · (Permalink)

Check out the Go code for that, absolute lunacy: https://yourbasic.org/golang/delete-element-slice/

rook2004 · 21 points · Posted at 15:02:46 on April 25, 2019 · (Permalink)

Programmers (during interviews): “show me how to delete an item from a linked list”

Programmers (other times): screws up array order to make removing efficient instead of using linked lists

quasicoherent_memes · 15 points · Posted at 18:38:00 on April 25, 2019 · (Permalink)

The most frustrating part of teaching undergrads functional or logic programming: they believe there is only one data structure and it‘s the array - everything else is just some shitty facsimile of arrays that you’re forcing them to use.

[deleted] · 19 points · Posted at 18:48:56 on April 25, 2019 · (Permalink)

[removed]

EmotionalAlbatross · 12 points · Posted at 01:21:56 on April 26, 2019 · (Permalink)

An array is a JSON object of the form

{
 "1": A,
 "2": B,
 "3": C, …
}

If you feel fancy, you can also use a linked list, which is a JSON object of the form:

{
 "head": A,
 "tail": {
   "head": B,
   "tail": {
     "head": C,
     "tail": …
    }
  }
}
MyNameIsErr · 11 points · Posted at 04:13:01 on April 26, 2019 · (Permalink)

You tried to jerk, but accidentally described exactly how arrays work in Lua. I respect that

KruppeBestGirl · 4 points · Posted at 07:13:28 on April 26, 2019 · (Permalink)

lol no 0-indexing

rook2004 · 2 points · Posted at 19:32:03 on April 25, 2019 · (Permalink)

You use arrays for functional programming? We always had some kind of list.

Edit: oh, you mean this ^ is hard to teach. Understood.

Bobshayd · 2 points · Posted at 18:27:24 on April 25, 2019 · (Permalink)

And there are efficient constructions which let you do it in amortized almost constant time! Actually, there are efficient constructions which let you do it in actual almost constant time!

wubscale · 13 points · Posted at 15:06:48 on April 25, 2019 · (Permalink)

/uj I've actually used swap/pop_back for deletion a few times. When order clearly doesn't matter, it's a conveniently-shaped hammer.

/j You can tell it's idiomatic Go code because all of the variable names are a single character.

samnardoni · 2 points · Posted at 17:10:01 on April 25, 2019 · (Permalink)

Even worse when it’s O(n2)

OctagonClock · 76 points · Posted at 09:01:22 on April 25, 2019 · (Permalink)

/uj Seeing del in Python code invokes visceral horror in me 99% of the time.

THICC_DICC_PRICC · 23 points · Posted at 14:53:06 on April 25, 2019 · (Permalink)

I’ve literally never seen it used ever. This post is legit the first time I’ve seen this command

annoyed_freelancer · 17 points · Posted at 15:02:34 on April 25, 2019 · (Permalink)*

/uj

I've preferred arr.filter(whatever) over del/arr.pop() ever since a real fucking nightmare with object mutation in a dumpster codebase I inherited.

OctagonClock · 7 points · Posted at 15:01:01 on April 25, 2019 · (Permalink)

Yeah, that's one of the reasons why.

RoughMedicine · 10 points · Posted at 18:26:46 on April 25, 2019 · (Permalink)

I've never used it for arrays, but I definitely use it to remove keys from dicts.

OctagonClock · 4 points · Posted at 18:50:48 on April 25, 2019 · (Permalink)

d.pop(key)

also, >mutating dicts

pingiun · 18 points · Posted at 20:54:37 on April 25, 2019 · (Permalink)

Also

> mutating

jtayloroconnor · 64 points · Posted at 11:29:21 on April 25, 2019 · (Permalink)

they missed the opportunity to convey information in the keyword, obviously it should be constantTimeOrderPreservingDel

IDoCodingStuffs · 38 points · Posted at 13:17:21 on April 25, 2019 · (Permalink)

constantTimeOrderPreservingDelBeanProviderInterfaceServiceComponentFactorySingletonAbstractEnumControllerFactoryBeanOrchestrator

FTFY

griever989 · 27 points · Posted at 13:27:35 on April 25, 2019 · (Permalink)

not using a Strategy pattern in 2019

How do you even expect your code to scale?

[deleted] · 2 points · Posted at 18:16:39 on April 25, 2019 · (Permalink)

imagine knowing more "design" snake oil "patterns" than just singleton and factory L O L

real programmers like me get shit done. not get lost in self-felating theories that produce no LOCs

tubbshonesty · 25 points · Posted at 13:34:56 on April 25, 2019 · (Permalink)*

Boost::Container::List::Erase<Boost::Container::List::Tags::ConstantTimeOrderPreserving>

FTFY

Come to the C++ world for maximum transparency.

/uj

This is actually somewhat similar to how boost-intrusive allow you to specify behaviors for intrusive linked lists, with regards to whether you want auto-unlink hooks or whether you require size() to be constant time.

porjolovsky · 6 points · Posted at 13:15:18 on April 25, 2019 · (Permalink)

constantTimeOrderPreservingDeleteFunction

FTFY

Causeless · 2 points · Posted at 13:29:05 on April 25, 2019 · (Permalink)

/uj

del in Python isn't constant time O(1), it's linear time O(n), because it needs to shift other things in the list to fit the newly created gap

hexane360 · 5 points · Posted at 16:55:11 on April 25, 2019 · (Permalink)

/uj

del in Python just calls a method. What it actually does depends entirely on the __delete__ method. For hash maps, for example, this is definitely constant time.

jtayloroconnor · 3 points · Posted at 16:09:52 on April 25, 2019 · (Permalink)

sorry, I've never used python and I've never needed it

n3f4s · 5 points · Posted at 14:21:12 on April 25, 2019 · (Permalink)

/uj

Aren't python's list linked list? If so, it shouldn't have to shift thing and del can be O(1).

[deleted] · 12 points · Posted at 14:58:41 on April 25, 2019 · (Permalink)*

[deleted]

n3f4s · 12 points · Posted at 15:13:17 on April 25, 2019 · (Permalink)

/uj

I learned something today (until I forget that because I rarely write Python).

Causeless · 8 points · Posted at 14:57:11 on April 25, 2019 · (Permalink)

/uj

No, they use variable length arrays.

igo95862 · 3 points · Posted at 16:17:32 on April 25, 2019 · (Permalink)

Python has a deque container which is a double linked list. It is useful for O(1) appends and pops from both ends.

irqlnotdispatchlevel · 50 points · Posted at 12:21:38 on April 25, 2019 · (Permalink)

Go doesn’t have a similar utility function.

You can't mess things up if you don't do things.

jk_scowling · 21 points · Posted at 12:48:03 on April 25, 2019 · (Permalink)

tapsForehead.jpg

DuBistKomisch · 15 points · Posted at 11:29:07 on April 25, 2019 · (Permalink)
JayCroghan · 8 points · Posted at 14:44:47 on April 25, 2019 · (Permalink)

Write an experience report

190n · 6 points · Posted at 21:11:24 on April 25, 2019 · (Permalink)

Where's the person who put generics in Go with some weird Unicode characters that look exactly like angle brackets?

edit: found it, lol

If you look closely, those aren't angle brackets, they're characters from the Canadian Aboriginal Syllabics block, which are allowed in Go identifiers. From Go's perspective, that's just one long identifier.

[deleted] · 2 points · Posted at 14:53:46 on April 25, 2019 · (Permalink)
type void* interface{}
lol-no-monads · 13 points · Posted at 13:08:25 on April 25, 2019 · (Permalink)

your you're basic

FTFY

BigFatMonads · 8 points · Posted at 14:35:07 on April 25, 2019 · (Permalink)

The Go code fmt.Println(1, 2, 3) prints three numbers. The code certainly is quite readable, but not so transparent. It is not clear if the time complexity is constant or linear.

Hint: it's linear

Why is this even an argument if the Go standard library uses for loops everywhere? Any function application looks like O(1), but sometimes it's actually O(n). If gophers weren't so hypocritical, there wouldn't be any for loops at all in the standard library.

haskell_leghumper · 8 points · Posted at 12:35:24 on April 25, 2019 · (Permalink)

It's certainly constant because it looks like a single operation.

Testiclese · 3 points · Posted at 19:07:28 on April 25, 2019 · (Permalink)

Experienced Gopher detected!

PC__LOAD__LETTER · 7 points · Posted at 15:00:19 on April 25, 2019 · (Permalink)

set +jerk

If you’re using a high level language and (a) care about the time complexity of array operations or (b) are concerned whether the implementation is same and you don’t know how to write a test, then don’t use that language.

set -jerk

voidvector · 2 points · Posted at 21:07:17 on April 25, 2019 · (Permalink)

Simple, if you del Python2, Python3 becomes Python2.

[deleted] · 1 points · Posted at 20:07:52 on April 25, 2019 · (Permalink)

Opacity is the new transparency.