Python For Loops & Dictionaries | Learn Python From Scratch 5 | Kovolff
Channel: prelearn
Date: 30/10/2020

In addition to lists, python offers another sequence, namely the dictionary.
A dictionary is sequence of key / value pairings, which are bound by curly braces.

ie abc = {‘name’: ‘Frank’, ‘surname’: ‘Brown’, ‘age’: 32}
This list has 3 elements.
Key of first element is name and value is frank
Key of second element is surname and value is Brown
Key of third element is age and value is 32

As you can see a dictionary can hold elements of different value types.

Accessing a element in a dictionary is done via its key, ie abc[‘surname’] gets you ‘Brown’
Or abc[‘age’] produces 32.

Elements in a list are always ordered the way you had set the order. 
In dictionaries the order of the elements is always changing depending on how python manages its memory. 

Yet the order in a dictionary is not as critical as in a list. In contrast to a list you do not access an element through its index (i.e. order in the list) but through its key - hence making the order of the elements of no importance.

Iterating through a dictionary is very similar to iterating through a list
ie  for my_key in abc:
 print(str(my_key) + ‘: ‘ + str(abc[key]))

my_key is a random name for variable denoting the key of each element you’re iterating through
abc is the dictionary

#python #for #dictionaries