Python Dictionary and List

Geomario
Oct 16, 2020

Python dictionary and list within a code are beyond belief. When starting to learn to code, it would be unclear the differences between lists and dictionaries. Lets code!

#Listsmake  = ["BMW", "Ford", "Tesla"]          #(1)
model = ["Serie 1", "Mustang", "Model 3"] #(2)

In the previous code block, we have created two lists. List make (1), contains three-string items with three autos make. The list model (2), have three items with three strings with the auto models. Lists are ordered sets of objects while dictionaries are unordered sets.

#Dictionaryautos = {"BMW":"Serie 1", "Ford":"Mustang", "Tesla":"Model 3"} #(1)

A Dictionary is an unordered set of items; the previous code block shows a dictionary (1). The main difference between lists and dictionaries is that you access the dictionary values from its key and not via their position. Therefore any value of a dictionary is associated or mapped to a value.

#Dictionaryautos = {"BMW":"Serie 1", "Ford":"Mustang", "Tesla":"Model 3"} #(1)autos["BMW"]  #(2)>>> "Serie 1" #(3)

The code block represents the association between the key : value pair in a dict. When printing the key “BMW” (2), you get its value “Serie 1” (3). A Python list can be accessed only via its position. There is also a way to convert a Python List to a Dict. We will talk about it in the next post. In the meanwhile, I was hoping you could give it a look into the Dictionary explanation from my Youtube channel.

Thanks &

Happy Coding!

What is a Dictionary? FreeDataScientist Youtube channel

--

--

Geomario

👨‍💻 Software & Data Developer | Software Research Engineer | MLE