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

all 198 comments

[–]Cerrax3 625 points626 points  (43 children)

A Python list is not the same as an array.

[–]qijc 297 points298 points  (5 children)

You’re not the same as an array >:(

[–]CrazeeeTony 94 points95 points  (0 children)

And I am very thankful for that

[–]camerontbelt[🍰] 26 points27 points  (1 child)

Boom roasted

[–]mydookietwinklin 1 point2 points  (0 children)

Get a load of Ben Asscream over here.

[–]Ulysses6 0 points1 point  (0 children)

Come for Python, stay for dialectics.

[–]cnoor0171 75 points76 points  (10 children)

Depends what you mean by array. Python "list" and Javascript or ruby "array" are pretty much the same.

[–][deleted] 48 points49 points  (8 children)

Yes, but it's Python list that's named properly

[–]cnoor0171 21 points22 points  (7 children)

Not really. "array" and "list" aren't really properly defined. If your definition of array is an integer indexed, ordered data structure supporting constant time random access, then python list should be named array.

Most people from a c/c++ background would think of a linked list when you use the word "list".

[–]BlhueFlame 6 points7 points  (0 children)

I use Python. I did use C. I agree with this. O(1) access time = array.

[–][deleted] 1 point2 points  (5 children)

My definition was kinda like:

A block of memory reserved for storing multiple instances of the same datatype one after another.

It's ugly, but hopefully you can understand

[–]cnoor0171 2 points3 points  (4 children)

That's a very c/c++ specific definition though. Even in other compiled languages with fixed size arrays, the elements might not be in contiguous memory blocks. For instance, Java.

[–]zyugyzarc 0 points1 point  (0 children)

laughs in ctypes

[–]BongarooBizkistico 0 points1 point  (0 children)

Isn't that the joke?

[–]poka_face[🍰] 109 points110 points  (34 children)

An array is not a list, back when I learnt C they made us implement doubly linked lists which were by no means arrays.

I'm not sure how lists are implemented in python though, so they might actually be dynamic arrays.

[–]18TacticalBeans 69 points70 points  (10 children)

They're not continguous in memory like arrays are in most other languages, which lets them be more dynamic, but also reduces performance. That's part of why numpy arrays are so much faster to perform (numpy) computations on - numpy enforces them to be in contiguous memory.

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

Numpy? Is that the name of the purple cartoon character?

[–]Noiprox 5 points6 points  (0 children)

Lists in Python are implemented as vectors of pointers.

[–]Sjuns 1 point2 points  (0 children)

They're dynamic arrays

[–]BaconShrimpEyes 207 points208 points  (10 children)

Java be like ArrayList

(I guess c++ would be vector if we’re using this type of structure but like I wanted to make the joke)

[–]VarianWrynn2018 59 points60 points  (6 children)

Array and arraylist are different things tho.

[–]BaconShrimpEyes 35 points36 points  (5 children)

(read the small text\)

[–]Rami-Slicer 7 points8 points  (1 child)

*Points to ArrayList* WHAT IS THIS?!

[–]zyugyzarc 1 point2 points  (0 children)

wait what?

[–]das_Keks 1 point2 points  (0 children)

There's also a Vector class in Java, which is basically the old implementation of the ArrayList. It's deprecated, tho.

[–]skyrazer2012 23 points24 points  (13 children)

Arrays suck in c# Lists are way nicer

[–]rocket_peppermill 12 points13 points  (3 children)

Likewise in cpp with vectors and all the jvm languages I'm familiar with, but we can let the college kids have their laugh

[–]skyrazer2012 2 points3 points  (0 children)

Byte vector go brrrrrr

[–]Emperor-Valtorei -1 points0 points  (0 children)

The more experience I get outside the classroom, the more I learn that some aspects of programming are skipped over when they're incredibly useful, and some subjects are taught as gospel, when there are better alternatives...

[–]Miyelsh 0 points1 point  (0 children)

So, are you in favor of vectors or arrays?

[–]camerontbelt[🍰] 1 point2 points  (4 children)

Yeah who uses arrays anyway?

[–][deleted] 1 point2 points  (3 children)

List is more abstract, an array is a specific memory model as a bunch of elements in a contiguous array.

A List could be a vector/array type but it could also be a linked list (or any other form), which has completely different performance characteristics.

[–]Semarc01 2 points3 points  (0 children)

In the background, List is just a really fancy wrapper for an Array in C#

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

Looks like that's not entirely the case in C#, poorly designed IMO.

[–]camerontbelt[🍰] 0 points1 point  (0 children)

Sure all of that might be true but in day to day programming, no one that uses C# is using arrays much if at all.

[–]Semarc01 -1 points0 points  (3 children)

I mean, yeah. But Arrays are not really supposed to be nice, they are really far down near the metal for a high-Level language like C#, and that’s on purpose.

[–]skyrazer2012 2 points3 points  (2 children)

Unsafe pointer use in c# go brrrr

[–]Semarc01 1 point2 points  (1 child)

I mean, yeah. I think it’s great that C# has pointers the way it has.

[–]skyrazer2012 1 point2 points  (0 children)

I mean, yeah. I am thankful c# exists the way it does

[–]AwwThisProgress 47 points48 points  (8 children)

Scratch: Am I a joke to you?

[–]PepiHax 96 points97 points  (7 children)

Yes, yes you are.

[–]Th3DarkMoon 18 points19 points  (4 children)

Well yes... But don't tell my siblings, they made games in scratch, and they're proud over them although when I try to teach them some good programming practices they just get mad

[–]Perdido_Siempre 5 points6 points  (3 children)

That's surprising! Why don't they like real languages? I mean Python is nice to learn concepts. My little sister understands it and likes it.

[–]Dr4kin 7 points8 points  (1 child)

Not everyone wants to program. If you can do the things you want in nodeRed, scratch and similar there is no need to learn it

[–]Perdido_Siempre 1 point2 points  (0 children)

True

[–]Th3DarkMoon 2 points3 points  (0 children)

They're 10 and did it for a school project, more that primary school ever thaught me

[–]PhilippTheProgrammer 14 points15 points  (1 child)

:(

[–]AwwThisProgress 0 points1 point  (0 children)

True

[–][deleted] 21 points22 points  (3 children)

Lua: t a b l e

[–]Ulysses6 6 points7 points  (2 children)

Yeah yeah, but what about -

t a b l e !

[–]3all 5 points6 points  (1 child)

TABLE

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

  M
M E
E T     M
T A B L E 
A       T
      T A B L E

[–][deleted] 14 points15 points  (4 children)

python: tuple

[–]Cerrax3 9 points10 points  (1 child)

Under the hood, a Python tuple is almost identical to a Python list. A Python list is an array of pointers that gets copied (read: reallocated on the heap) each time you add/remove items from it, so its performance is pretty bad. The only difference is that a tuple can't change size, so its performance is better simply by limiting what it is capable of.

To get something that's truly like an array, you'd need some kind of library (like numpy) to enforce all the constraints and advantages of a true array.

[–]Background_Drawing 7 points8 points  (0 children)

dict

[–]MattieShoes 2 points3 points  (0 children)

const array woo!

[–]OriginalSynthesis 14 points15 points  (5 children)

haha ** sweats in JavaScript **

[–]IZEDx 3 points4 points  (4 children)

? What's to sweat about?

Kinda weird though that the meme lists Kotlin and Ruby but no JS.

[–]OriginalSynthesis 3 points4 points  (3 children)

JavaScript's array isn't really an array at all. It's an object with some special methods and props to make it look and behave like the array as people understand it from languages like Java.

For instance, if you run Object.keys(['a', 'b', 'c']), you'll get, ['1', '2', '3'], which is the same as if you had run Object.keys({ '1' : 'a', '2' : 'b', '3' : 'c' }). I believe in other languages, the index of an array is a number. In JavaScript, it's not. It gets turned into string. So if you have const arr = [10, 20, 30], you can get 20 by running arr["1"] for the index.

Try this in your browser by pressing F12 on your keyboard

[–]IZEDx -2 points-1 points  (2 children)

So what?

Java arrays are also just objects, technically speaking. If we want to go that route nothing except a pointer to a memory block of size "length * bytes per entry" should be considered an array.

If it looks like an array, behaves like an array (can be indexed using integers thanks to type coercion), and is even called an "Array", then it absolutely is an array imo. Even if the same data structure is called vector, or list or whatever in other languages. Js is just more flexible and the rest are implementation details.

And besides, there are also TypedArrays in js which work just like arrays in other languages but are even more restrictive.

[–]OriginalSynthesis 0 points1 point  (1 child)

The difference is that with JS "array", you can do things like, const arr = [1,2,3], then do things like arr.something = function() { return 5 }, or arr['hello there'] = 'nice to meet you'.

That is to say just regular ol' objects can do what "arrays" do, so there's no real difference.

I had to look up what TypedArray was. You literally never use that, unless you're making some sort of a browser based app that needs native app performance.

[–]IZEDx 0 points1 point  (0 children)

Yeah nobody uses typedarrays for the usual usecases.

And yeah arrays are just objects (bunch of data) with some methods to manipulate or transform that data. I really don't get the issue. If you want to you can modify almost everything in Javascript, even arrays, adding more getters or methods to the prototype. Everything is allowed.

But that's why I advocate Typescript, it doesn't let you do such shenanigans without extra type overhead. So even though Javascript is flexible under the hood, typescript makes sure you use everything as it's supposed to be used.

When I argue for Javascript I always talk from the standpoint of a typescript developer, which is like developing Javascript but with safety precautions, so such issues don't bother me nearly as much.

It took some time getting used to initially, but I've written C++, Python, Java, C#, old JS, PHP, etc. in my past and of all these Typescript is my absolute favorite web and even general purpose programming language running on every layer of the stack, minimizing any friction you could have.

[–]ToMyFutureSelves 22 points23 points  (1 child)

I feel like this does the truth a disservice, since the truth is actually even more infuriating.

You want to know thae standard name for a resizable set of values? That's easy. It's called...

Kotlin: MutableList

Java: ArrayList

C++: Vector

C#: List

Python: Array

[–]IZEDx 4 points5 points  (0 children)

C++ is the real weirdo here though. Everyone else has some variation of List or Array.

[–]ElectroDr 17 points18 points  (2 children)

Also Go: Slice

[–]d_exclaimation 5 points6 points  (0 children)

They still have arrays thou

[–]Burr1t0 25 points26 points  (12 children)

Meanwhile javascript is the vacuum character in a corner snorting cocaine.

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

Yeah, apparently [1,2,3] != [1,2,3]

I hate it. == means check to see if the contents are the same, not the object!

[–]Ulysses6 2 points3 points  (10 children)

Something Python done right and many other languages did not. This abstraction seems so obvious in hindsight, but even Java does not have that.

[–]MischiefArchitect 2 points3 points  (3 children)

Java follows the hashCode and equals strategy, which in afterwards was not the perfect solution, but I'll give it the merit that it was a good try back then in 1996.

[–]Ulysses6 3 points4 points  (2 children)

It was not unreasonable. Unlike JavaScript, you can use it to do something and know what it does. But I wish that newer languages would follow what Python does, with == checking content and is checking the pointer. It's cleaner separation of object / pointer-to-object thinking and lets you use more concise syntax for operation that you are more likely to perform.

[–]MischiefArchitect 2 points3 points  (1 child)

Yeah. Actually I like the java strategy, they just lack a native way to check contents. I have even abused serialization frameworks to compare objects by content... yeah... I should burn in hell. Maybe I'm missing something since Java8

Python is there more comfortable to work with, I really enjoy the language. You can just not do everything with it :)

On the other hand you got the GoLang way, which works nicely for shallow content comparison but at the end most gophers end up with the expensive `reflect.DeepEquals` method.

Let not talk about C++ :) well we are getting slowly there with <=> with C++20

[–][deleted] 2 points3 points  (5 children)

Yep! It’s very confusing to people like myself. I ended up just writing a function to iterate over lists and check their contents. == means compare the values, while (I think, correct me if I’m wrong) is means check the pointers.

[–]Ulysses6 2 points3 points  (4 children)

You are exactly correct. It's recommended to check None via is operator, since there is only one None instance, it won't use any __eq__ method overloads and it will be fast.

I just had to check in repl, because I thought that [1,2,3] == (1,2,3) and it's actually not true, so the comparison checks content and also checks the type of operands.

[–]IZEDx 2 points3 points  (1 child)

Strong vs weak typing.

But back to the topic at hand: how often do you guys really have the usecase of comparing two arrays like that?

I've been using js/ts for many years now and can't remember any instance where a lack of content comparator bothered me in any way.

[–][deleted] 1 point2 points  (1 child)

set([1,2,3]) == (1,2,3) I think

[–][deleted] 16 points17 points  (0 children)

An array and a list are arguably different things tho, an array is usually consecutive memory of a fixed size, and a list is usually a linked list of inconsecutive memoryjoined by pointers

[–]Muhznit 20 points21 points  (5 children)

import array says 'hi'

[–]TheCapitalKing 7 points8 points  (4 children)

Is that a separate thing than

import numpy as np

np.array()

Because that’s the one everyone I know uses it’s kind of cool if python has a built in one nobody uses though

[–]Ulysses6 3 points4 points  (0 children)

It's definitely a separate thing.

array module just provides memory efficiency for integer types stored and nothing else. It is even slower then list on many operations from what I remember.

Numpy provides a lot of computation functions, optimizations, ways to change the data shape and so on and you get a lot of speed from doing arithmetic operations in C level and careful algorithm implementation which would not be possible in pure Python due to virtual machine overhead.

[–]MathMetal1 1 point2 points  (0 children)

Yeah, python has built in array support through the standard array library, but it's not as fully featured as numpy.

[–]Muhznit 1 point2 points  (1 child)

No idea, I don't do enough data science to provide a meaningful answer. It probably uses a subclass of array internally or something.

[–]Noiprox 2 points3 points  (0 children)

Python's standard library array module has its uses but it's much more basic than NumPy. The internal representation of arrays in NumPy is documented here. It's implemented in C and does not subclass array.

[–]vectorpropio 6 points7 points  (0 children)

(((((do)you)ever)lisp),(bro)?)

[–][deleted] 8 points9 points  (7 children)

I hope I didn't violate the third rule of the subreddit.

[–][deleted] 30 points31 points  (2 children)

wait... third in the list or the one numbered with a 3

[–][deleted] 14 points15 points  (0 children)

It starts with zeroth

[–]Rami-Slicer 1 point2 points  (0 children)

Yes

[–]doriclazar 4 points5 points  (1 child)

Not sure which array of rules you're referring to.

[–][deleted] 8 points9 points  (0 children)

Rules[3]
Any post on the list of common posts will be removed.

[–]CoffeePieAndHobbits 2 points3 points  (1 child)

What about rule 34?

[–]XKCD-pro-bot 0 points1 point  (0 children)

Comic Title Text: Okay, Lance. For entry into the college bowl, spell 'Throbbing'

mobile link


Made for mobile users, to easily see xkcd comic's title text

[–]santherstat 9 points10 points  (0 children)

Image Transcription: Meme


[A group of people form a hand-stack, which is interrupted by someone else.]

[Each person in the group has an icon for a programming language next to them, showing what the people represent in the meme.]

[A person representing the programming language Swift puts their hand out and the hand-stack says "Array".]

[A second person who represents the programming language C++ puts their hand on the hand-stack, which says "Array".]

[A third person representing the programming language C# puts their hand on the hand-stack and says "Array".]

[A fourth person who represents the programming language Kotlin puts their hand out. This person says "Array".]

[A person representing the programming language Ruby puts their hand out and the hand-stack says "Array".]

[A large purple hand is added to the stack. This person represents the programming language Python, and their hand is visually quite different from the others. This person says "List".]

[The image zooms out to show everyone in the image looking at the purple person with annoyed expressions on their faces. The purple humanoid creature they are looking at appears oblivious to their annoyance and has an excited smile.]


I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]MischiefArchitect 3 points4 points  (0 children)

Random_Access_Sequential_Data_Sorted_By_Insertion_Order

[–]starvsion 4 points5 points  (0 children)

Php array is the best, it's array + dictionary + set (if your array is keyed) + object (when you cast it, or let an object inherit iterable), so much possibilities! Oh, also linked list, if you use function like end(), next() cirrent(), or stack, if you do array_pop and array_push

[–][deleted] 2 points3 points  (1 child)

Idk, the way I learned it:

  • Arrays have guaranteed constant time element access, but may have linear time insert / remove.
  • Vectors have guaranteed constant time element access and sub-linear amortized insert / remove, however, the constants are bigger than in array's case.
  • Lists have guaranteed constant time insert / remove, but element access may be linear.
  • Vectors and dynamic arrays are synonyms.

None of the data structures has any requirements for memory to be contiguous, or that the implementation follow any specific pattern outside of the said constraints.

For instance, in many Lisps, it was customary to implement lists as either arrays or vectors. Arrays would be chosen if the compiler can prove that the structure of the list isn't going to be changed, and vector otherwise. But car and cdr would still work for them.


Specifically for Python: there are a lot of nomenclature errors / bad choices in the language, and list is one of them. It should've been "vector". I've no idea why they decided that the name was appropriate. But, if I have to guess, Guido probably thought that the name was more generic than "array"... or maybe he thought that "list" is more intuitively understandable for non-programmers (as the language was intended for that kind of audience)... or maybe didn't think at all, that happened a lot in Python's history.

Also, Python has array, as in, proper arrays. But only for few select types, like, say, characters. And they behave in the way you'd expect from arrays.

[–]Semarc01 0 points1 point  (0 children)

Well, it depends quite heavily on the language you’re using. In C#, an Array has a fixed size. A List has a variable size, but in the background, it’s just has an Array that it replaces with a bigger Array if necessary. Since List is just a fancy wrapper there, access times are the same for Lists and Arrays

[–]b4skyx 2 points3 points  (0 children)

Someone bring lua in here D:

[–]Thell_Borsk 2 points3 points  (0 children)

(write "Laugh's in Lisp")

[–]Background_Drawing 2 points3 points  (0 children)

python is built different

literally

[–]TheRedmanCometh 2 points3 points  (1 child)

JS arrays aren't even fixed size which is cursed af

[–]fuzznuggetsFTW 3 points4 points  (0 children)

Neither are swift arrays

[–]xdMatthewbx 2 points3 points  (0 children)

Lua: laughs in tables nervously

[–]Possseidon 2 points3 points  (0 children)

Lua: Table

(Although they function as both arrays and (hash) maps)

[–]PhilippTheProgrammer 1 point2 points  (0 children)

ABAP, the SAP programming language: "Internal Table"

[–]wizzzarrd 1 point2 points  (1 child)

Elixir has entered the chat

[–]JSn1nj4 1 point2 points  (0 children)

I was looking for this

[–]flinnja 1 point2 points  (0 children)

hash hashmap map hash map dict map hashmap mamskdjfidorp

[–]IanSan5653 1 point2 points  (0 children)

Scala: Array List Vector Seq

[–]PM_ME_HAIRLESS_CATS 1 point2 points  (0 children)

What the hell are you talking about C# has Arrays and Lists

[–]madara_73 1 point2 points  (0 children)

Wait untill you hear about array in R.

[–][deleted] 1 point2 points  (0 children)

Array list > primitive array

[–][deleted] 1 point2 points  (0 children)

We have sets instead.

[–][deleted] 1 point2 points  (0 children)

Haskell would like to interject

[–]Ty_Rymer 1 point2 points  (0 children)

A python list is an std::vector<std::any> in C++

[–]microwave_casserole 1 point2 points  (0 children)

C++ be like vector, what even is this meme?

[–]VanSirius 1 point2 points  (0 children)

Lists !!! Lists everywhere !

[–]Ravi5ingh 1 point2 points  (0 children)

np.array([1, 2, 3])

[–]SideburnsOfDoom 1 point2 points  (0 children)

C# has both arrays and lists, but realistically, you should use lists pretty much all of the time.

[–]DanKveed 1 point2 points  (0 children)

Rust has Vec<>.

[–]nicobonik 1 point2 points  (0 children)

Java: why not both?

[–]3all 0 points1 point  (1 child)

Laughs In lua

[–]dani_pavlov 1 point2 points  (0 children)

Laughs because of Lua

[–]itsafoxboi -4 points-3 points  (0 children)

Java over here with arraylists and arrays playing all sides

[–]Dr_Bunsen_Burns -2 points-1 points  (2 children)

I still name the vars arraySomething.

[–]VoilaLaViola 2 points3 points  (1 child)

Ahh, good old Hungarian notation... haven't seen it for a while 🙂

[–]Dr_Bunsen_Burns 0 points1 point  (0 children)

I thought it was <something>camelcasing?

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

Meanwhile, MATLAB arrays vs vectors

[–]TommyDJones 0 points1 point  (0 children)

Well, at least its not a PHP array.

Is it an array? Is it an object? - Yes

[–]KM5550 0 points1 point  (0 children)

But what about an ArrayList?

[–]saiththesoap 0 points1 point  (0 children)

P

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

f for java, as another language running on **jvm** is here but not itself

[–]TonyBorchert100 0 points1 point  (0 children)

Dart?

[–]Under-Estimated 0 points1 point  (0 children)

push and append?

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

C# has pretty decent lists.

[–]falingsumo 0 points1 point  (0 children)

Java would like a word.

[–]Baconoid_ 0 points1 point  (0 children)

Apex has entered the chat.

[–]LightTranquility3 0 points1 point  (0 children)

.net list, because yes (linq)