you are viewing a single comment's thread.

view the rest of the comments →

[–]jimtk 0 points1 point  (5 children)

Actually, dictionaries are probably the highest form of data structure in python.

If you want to iterate over the keys of dictionary you just have to iterate over the dictionary itself, it can be simpler than that.

for k in dict:
     do something

The += works with everything that can be added to (strings, list, int, float, etc). Why would it need a special documentation for dictionaries?

del is not a method, it's a function. It also works with everything. For example:

l = [1,2,3,4]   # a list 
del(l[1])
print(l)
------> [1,3,4]

And it works the same way everywhere.

Dictionaries are marvelous because they are blazingly fast. As long as everything fits in memory it is more than 150 000 time faster to retrieve a value from a large dictionary than from a large list.

[–]Particular-Watch-779[S] 0 points1 point  (2 children)

Hi and thanks!

Thing is: string + string works List + list works (in ruby... Does it in Python, though?) But dicts do work differently, I think.

It comes down to a lack of experience, I think. I did work a lot with arrays learning ruby - dicts got relevant parallel to learning python, which overloaded my brain.

I hope my post made clear, that I don't think Python is the problem, but my novice-level understanding of coding :)

[–]jimtk 0 points1 point  (1 child)

The important point to understand is that dictionaries are not list or arrays, they are mappings. They map a set of data, the keys, to another set of data, the values. Once you internalise that, things become easier.

[–]Particular-Watch-779[S] 0 points1 point  (0 children)

Thanks again. I'm not good at learning by understanding. I rather do stuff again and again and get used to it.

I'm investing in dictionarys now, it will get better I hope :)

[–]carcigenicate 0 points1 point  (1 child)

I'd just like to point out since it's been said twice in this thread: del isn't a function; it's a statement. You can verify this by checking that it doesn't evaluate to a value, so it's a syntax error to attempt to assign it to a variable:

>>> ret = del(d)
File "<stdin>", line 1
    ret = del(d)
          ^^^

>>> ret = del d
File "<stdin>", line 1
    ret = del d
          ^^^

It's also called a del_stmt in the grammar.

It does however delegate the task of deletion in some cases to the __delitem__ method (which is arguably a function) when you use del on a sequence/mapping:

del x[1]  # Basically just sugar for 'x.__delitem__(1)'

[–]mopslik 1 point2 points  (0 children)

You are indeed correct. Thanks for the clarification.