Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

What exactly is Python-like about the data-structures? What do they do that, say, Java's Maps and Lists don't do?


sort by: page size:

Python has Lists (arrays), Dict, and Sets. That's pretty much it.

There's no linked lists. There's no sorted maps, no sorted sets, no maps that preserve insertion order, no queues, no priority queues, no bitsets. And there's no immutable collection in Python besides strings.

Meanwhile, Scala has all of that and more: https://docs.scala-lang.org/overviews/collections/overview.h...

Moreover, scala has synchronized collections that can work over multiple threads. I guess all collections work that way in Python, but that's because Python doesn't even support true thread parallelism in the first place!

Also, if we're talking about the number of methods on the collections, Scala has way way more. map/foreach/filter/foldl/foldr/option/drop/take/first/last etc etc.


Oddly that's one of the things I like most about python. The containers module has so many very useful things. Plus hash-tables and array-lists shouldn't ever really be the same thing.

> Python mostly scratches that itch for me, but even there it has multiple methods to do the same stuff within the standard library, like list.sort() and sorted(list)

There's also the list vs tuple thing. I can understand it from an efficiency point of view, but it isn't as consistent as just having one such data structure (e.g., a perl array)


This is a nice article, as a non-Python programmer who's dabbled a bit in order to read some random Python code, it gave me some appreciation for the "Pythonic"-way.

I have to say though, that the thing that astonished me the most about Python is the Java-like "closures". I sort of thought it would be like Ruby, which I thought was closer to Scheme and JavaScript, and then I realized that Ruby isn't quite like them either. (I know Ruby is close with its lambdas and blocks, but it's not intuitive to me.)

This isn't necessarily to the detriment of Python (or Ruby) but just something I observed when trying to explain Java's lack of closures to someone who only knows Python.


Thanks for the detailed response.

I was in fact taking it as almost a given that Python lists were backed by arrays under the hood.


Requiring a type definition, Python's Array type more closely matches Java and C# Lists than Python's List type.

In Python, Arrays are sequence types and behave very much like lists[1] In other words, even in Python, arrays have similar semantics to Javascript Arrays not Java Arrays.

[1] https://docs.python.org/3.4/library/array.html


Python has immutable data structures, lists just aren't one of them. (Tuples are. So are, unlike in some languages, strings.)

Kinda. Many things you can do with a map can also be done with a list comprehension or generator, so that seems to be the way to go.

Moving from Scala to Python it was annoying at first.. but honestly the code is easier to read in most cases, so no complaints here.


Nope. Can't stand that Python uses built-in functions for basic things like list, map, fold, etc... instead of methods on base classes. Here's a super basic example. Add one to an array [1,2,3] and print results.

Python: print(list(map(lambda x: x + 1, [1,2,3])))

Ruby: print [1,2,3].map {|x| x + 1}

So much more readable, easy to write, etc... Ruby keeps it consistent by making pretty much everything an object and you just call methods. Even Haskell has dot notation to chain functions since it's just so much easier to read and write...


> Python's semantics with the list type are a constant surprise to newcomers.

Care to elaborate?


Agreed, although it's interesting to see the disassemblies.

Python is a language about readability and conciseness first, performance second.

I would have liked to have seen `map` used in the list comp examples, as well as seeing the tests run on Python 3.


What Java, JavaScript and C# call "arrays" are each very much like what Python calls "lists'. In particular the indexing after push() and pop() type operations and the bigO of indexing.

More abstractly, Historically, in computing "list" connotes pointers and "array" connotes sequential memory. The connotations imply engineering tradeoffs. [1] "List" may make more sense for a beginning programmer. It is an arbitrary context switch for programmers writing in multiple languages. Considering that one of the driving use cases of Python has been systems programming, "list" is misleading regarding performance characteristics. [2]

[1]: For example as in Scala https://docs.scala-lang.org/overviews/collections/performanc...

[2]: googling "python arrays" returns a lot of results explaining the difference between Python's Arrays and Python's Lists. "List" is "foo => spam" and "bar => eggs" Pythonism gone too far.


I think Python is the worst-documented large language I use.

For ```list``` it says:

Lists implement all of the common and mutable sequence operations. Lists also provide the following additional method:

...where "common" and "mutable" are links.

Meaning: the methods of lists are strewn across three different location, four if you count the constructors above this sentence.


True, and I like python list alot as it offer the ability to hold heterogenous objects and it's really flexible & simple. Just happened to using list the other day to cache a large 2d array, which turned out to be slow. Certainly, my implementation has room for improvement, and my comparison isn't straightly an apple to apple. Taken down the post n modifying. Thanks for the comment btw.

It does? I think that actually makes it less clear what lists in Python are actually good or bad for. I think thinking of them in terms of a linked list in C is a much better abstraction.

Python also has filters maps and lambdas, and lots of third-party support for other functional ideas (e.g. the Toolz library).

Though personally I really like itertools and list comprehensions. (And list comprehensions are pretty functional as well, they were inspired by haskell I blieve.)


Python data structure manipulation/comprehensions. I realize there's some overlap with other languages, but this to me is Python's 'killer' feature.

Python also has map(), but the lack of lightweight block syntax (like Perl's and Ruby's) makes it more cumbersome to use. I don't see how list comprehensions are less powerful, though.

Thanks for all the good points. To be honest, I'm not sure what you're trying to say. I've been coding in python for years and Moka was built from my annoyance using functional paradigms with the stdlib.

As you clearly showed, Python doesn't have an uniform syntax to deal with this paradigm. I.e. there are lots of different constructs and, as you said, common idioms or patterns. Have you already argued with a Java programmer saying that these 'Design Pattern' are just a limitation of the language.. whereas in Python you'd probably just use a simple Dict (or whatever)? I'm sure you did. I feel the same with the idioms and patterns.

See, in Clojure (And Arc, and even Ruby), there is an uniform syntax.. whereas in Python we've got itertools, list comprehension, builtins map/filter, random builtins functino such as sorted().

I have to agree with you that Moka is not Pythonic in the There's only one way to solve a problem as we add a new way. However, Moka was created because there was so much inconsistent alternatives..

However, Moka is Pythonic in how it behaves. God knows I could have use all nifty hacks to make it behaves magically.. but I chose to take the explicit route by overriding list/dict. I could have used string interpolation for function (See http://osteele.com/sources/javascript/functional/); but instead went the Pythonic way with standard lambda functions. Maybe you are right about the operator keywords shortcut (i.e. using List().keep(operator.eg) instead of List().keep(eg=); However, I still feel it was a way to make it even easier to integrate with existing tools from the stdlib.

Lastly, you said:

    zen: flat is better than nested
    > Dict(a=1, b=2).update(c=3).rem(lambda x, y: x=='a')
^^^^^^^^^^ This is not nested, this is chained.

This, however, is nested: # Taken directly from the itertools stdlib page. next(islice(iterable, n, None, default)

    # Here, this is not nested.. this is chained:
    def logged_user(self):
        return (self.users
                      .keep(User.is_logged) 
                      .keep(lambda u: u.is_active)
                      .map(lambda x: User.objects.get(id=x)))
next

Legal | privacy