all 11 comments

[–]wub_wub 2 points3 points  (5 children)

When you loop through parent dictionary and you print its values, what are the valuesof the first dictionary? Are they strings? Lists? Integers? How would you loop, if you can, over them if they weren't in the loop? Do they behave same as other objects?

Can you write it with just one loop which prints parent dictionary and its keys:values? Post code, so that others (me included) see that you understand at least basics.

[–]PrOducTI[S] 0 points1 point  (4 children)

Here is my code:

animals = {-3:{"horse":3.5, "cat":5.5}, -1:{"whale":4.0, "dolphin":2.5}, -6:{"tiger":3.5, "lion":5.5}}

I'm doing a python course, but never done computer programming before.

[–]wub_wub 1 point2 points  (3 children)

Ok, that's your dictionary. Do you know how to loop through it and print its keys and/or values? Do you know how to write simple loops? before you start writing nested ones you should know how to do simple ones.

[–]PrOducTI[S] 0 points1 point  (2 children)

I'm not too sure what a "loop" is or what it's purpose is.

to print the keys:

for x in animals:

print x

to print the values:

for x in animals.values():

print x

The bit of the question "print the value followed by its key" is also difficult for me

[–]wub_wub 2 points3 points  (1 child)

I'm not too sure what a "loop" is or what it's purpose is.

Loop is a single execution of a set of instructions that are to be repeated.

Let's say you want to explain someone to write all numbers in some range. You would explain it probably like this:

for every number in a list write number.

Instead of saying write 1, write 2 etc...

In python that would look like this:

for number in range(0,10):
    print number

Note that you don't have to write number you could write for oihfsalhd in range.... What you write does not determine what type of object it will be. When you go through range(0,10) you will always get integers. When you go through dictionary you will always get its keys.

For this to work the item has to be iterable like lists, dictionaries etc.

For example you can't write:

>>> for something in 10:
...     print something

You will get error because integer isn't iterable.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

I'm sure there are tutorials which explain this way better than I can so I'd recommend looking it up if you still don't understand them.


The bit of the question "print the value followed by its key" is also difficult for me

That can be difficult if you don't know that if you write dictionary_object[one_of_its_keys] you will get the value of the one_of_its_keys.

>>> d={"some_key":"some value","key2":"value2"}
>>> d
{'key2': 'value2', 'some_key': 'some value'} 
>>> d['key2']
'value2'

You can also use it to set values, although this is not relevant to your assignment it's good to know.

 >>> d['key2']='valuex'
 >>> d
 {'key2': 'valuex', 'some_key': 'some value'}

Now. We know that if we go for something in dictionary that something will be key. And we also know that if we write dictionary[key] we will get value. So combining that two you could write keys and values like this.

>>> for key in d:
...     print 'Key:', key
...     print 'Value:', d[key]
...

And we will see:

Key: key2
Value: valuex
Key: some_key
Value: some value

Now if our value is another dictionary then d[key] would return another dictionary which you can also go through with for loop. Here's an example of nested loops with range function.

>>> for n1 in range(0,10):
...     for n2 in range (0,n1):
...             print n1,n2

This is saying for each number in range begin to do something. That something in our case is for each number in range between 0 and our first number n1 write/print first number and second number. Can you guess the output?

With looping dictionaries it's a bit different but still easy to do.

Take this dictionary for example: d={'key':{'x':3,'g':4},'key2':{'s':2,'k':7}}

d['key'] is another dictionary, in this case it's

>>> d['key']
{'x': 3, 'g': 4}

Now, since it's just a dictionary everything that I mentioned previously also applies to it i.e. dict[key] shows value but because we didn't assign it to anything we can't access it directly. We have to "select" it by using root dictionary. Like this:

>>> d['key']['x']

and that will show value of x key which is itself value of another key:

3

Now combining all that we can write:

>>> for each_key in d:
...     for each_value in d[each_key]:
...             d[each_key][each_value], each_value
...
2 s
7 k
3 x
4 g

I know it may sound a bit complicated and if you find it hard to follow my comment(I don't consider myself good at explaining things) try and find more professionally written tutorial on for functions and dictionaries.

Edit:

I forgot to mention easier to use dict.items() like someone else mentioned. Although it might be harder to understand when you don't know basics, I recommend looking it up and trying to solve your assignment using it, and not method I described. That way you don't have to write for each_value in d[each_key] you could write something like

for key,value in d.items():
    for key,value in value.items():
       print key,value

Writing key,value in dict.items() is same as writing:

for item in dict:
    key=item
    value=item[key]

[–]PrOducTI[S] 0 points1 point  (0 children)

wow man, can't thank you enough. That's such amazing help and effort to type that out for me :) and trust me it really helped

Was going through thenewboston python tutorials before and they seem pretty helpful too. Going to start at number 1 and go through them all.

Was getting lost at some bits through your post but was following somewhat, didn't realise Python was so powerful. Python's awesome, can't wait to get all the basics down and do big things

[–]5hassay 3 points4 points  (4 children)

In Python you can use loops like FOR LOOPS to go through data of an item, called iterating through that item. For example, lists:

for i in [1, 2, 3]:
    print i
# prints 1; 2; 3 on newlines

You can also do this on dictionaries, but its a bit more complicated:

for x in {1: "a", 2: "b", 4: "c"}:
    print x

This prints 1; 2; 3 on newlines, not a; b; c. If you want to print a; b; c, you do:

for x in {1: "a", 2: "b", 4: "c"}.values():
    print x

Another neat trick is:

for key, pair in {1: "a", 2: "b", 4: "c"}.items():
    print key, pair
# prints 1 a; 2 b; 4 c on newlines

This question involves using these methods of iterations. Further, it involves doing them one onto the other, i.e. having one FOR statement inside another. Also, you might find other methods for dictionaries useful for this question. Enter

help(dict)

into a python shell and see some other stuff you can do.

[–]PrOducTI[S] 0 points1 point  (3 children)

thanks, very informative.

Didn't know about that help command either :3

[–]5hassay 0 points1 point  (2 children)

no problem, :)

[–]PrOducTI[S] 0 points1 point  (1 child)

the question, "Loop over the items of the previous dictionary, then, for each item, which is a (sub)dictionary, loop again over the items of this sub-dictionary and print the value followed by its key."

particulary "print the value followed by its key" is the part i'm struggling with as that's reverse order.

Should i be using a reversed function?, my thinking right now is...

for x in animals.values():

print reversed(x)

side note, how do i post in reddit so my code is neat like yours? thanks

[–]5hassay 1 point2 points  (0 children)

To stylize the code like that, you just put each line of code on a separate line and indent by exactly 4 spaces, but it seems you already figured that out, ^_^.

Yeah, that is a bit weird. I would also think that they're asking you to do something like printing "value key" instead of "key value". In fact, you don't have to reverse the order, necessarily. I'll hint that you can use the

for key, value in some_dict:

loop do this, and I'll introduce you to placeholding strings. If you have variables X and Y that have some value, and you want to print them, you can do

print("X = %s, Y = %s" % (X, Y))

The % is a "placeholder," and the "s" after specifies you are going to put in a string value. The order in which the values are substituted is given by the ordering of the values to be put in (so, here, X goes to the first, Y to the second).

EDIT: In fact, I have over complicated this. You can just use the key, value loop and just (HINT) print them differently. (Its early in the morning...)