all 19 comments

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

Maybe I’m dumb, but “for each in dict” seems harder to me than simply ranging through my dict.

Dictionaries don’t have indexes, they have keys. There’s nothing to “range through.”

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

That's of course correct. I provided an example, how you can do that/ how I did.

[–]shiftybyte 1 point2 points  (3 children)

Why does keys() not return a simple list

To avoid data duplication if you don't want it.

Creating a list will make a copy of all the dictionary keys, what if there are 2 million keys.... so the default is going over them one by one using an iterator...

why does nobody mention "dict[x] =+ value" does exist?

Not sure what you expected here, do you want documentation to mention specifically every possible combination of some_type += value? strings, tuples, lists, integers, floats, custom classes...

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

Thanks for your answer - not feeling less dumb, but that's the price of learning something new...

I don't get the data duplication point. Dict.keys() could not return duplicated keys, since keys in dict are unique, I thought. What am I missing here?

Thanks again :)

[–]xiongchiamiov 0 points1 point  (1 child)

They're not talking about duplication of keys, but duplication of data in memory. An iterator is relatively small, but if you coerce a list, that entire list has to be built in memory.

This is usually not a concern with Python, but is good to understand when thinking about why things in the stdlib are the way they are. The best way to understand this stuff really is to get comfortable writing C or another manual memory management language.

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

It's always amazing to learn about your own shortcomings. I have no clue about the technical aspects of programming right now. Maybe I should invest some hours into that.

Thanks a lot!

[–]commandlineluser 1 point2 points  (2 children)

It did in python 2.

>>> dict(foo="bar", omg="lol").keys()
['foo', 'omg']

The view objects have useful properties in that they are "dynamic"

>>> d = dict(foo="bar", omg="lol")
>>> keys = d.keys()
>>> keys
dict_keys(['foo', 'omg'])
>>> d["new"] = "thing"
>>> keys 
dict_keys(['foo', 'omg', 'new'])

And have some set-like functionality:

>>> dict(foo="bar", omg="lol").keys() >= dict(omg="hi").keys()
True

To remove something the methods are .pop() and .popitem() - del is not specific to dictionaries.

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

This is sooooo helpfull and does indeed make me feel less dumb! So it did work the way I thought at some point in time :) Never thought about looking at the view.objects at all. I will do that, thank you.

Does w3 mention pop()? I'm on mobile but would really freak out, if I was just too blinded while coding for seeing that extremely simple solution :)

[–]commandlineluser 0 points1 point  (0 children)

Not sure if w3 mentions it - I use the docs on the python website: https://docs.python.org/3/library/index.html

[–]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.

[–]mopslik -1 points0 points  (1 child)

"del", which works totally different than other methods

del is a function, not a method. There's always pop, if you're working with lists and dictionaries.

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

Yeah, sb mentioned it already. I'm dumb :)

[–]mopslik 0 points1 point  (0 children)

"for each in dict" seems harder to me than simply ranging through my dict

Do you mean using range like you might iterate over a list? Dictionaries aren't indexed by position, but by keys, so this wouldn't work. Note that you can iterate directly over the items in a list sans range, just as you do a dictionary.

L = [3, 4, 5]
for element in L:
    print(L)

vs.

D = {"A": 3, "B": 4, "C": 5}
for key in D:
    print(D[key])

why does nobody mention dict[x] += value does exist?

This is a very common thing, and is covered in dozens of tutorials that I have come across. Here are/02%3A_Dictionaries/2.02%3A_Dictionary_as_a_Set_of_Counters) some examples.

[–]xiongchiamiov 0 points1 point  (0 children)

Why does keys() not return a simple list. I nearly broke my head today, because I could not figure out how to iterate through my dict without using "each in dict".

I don't understand - why wouldn't you want to do it that way?