+ 2

How can we use properly “list”,”dictionaries” in Python?

I have been programming Java in 10 years, but I am new to programming Python. So, I want to appreciate the difference between the title’s two objects. Are there any specific reason to use dictionaries better than list ?

14th Jan 2018, 6:20 AM
Kiyo
Kiyo - avatar
2 Respuestas
+ 9
Dictionaries are a lot more different than lists in python The former is like switch statement in java and the latter is like arrays in java Dictionary contains elements in the form of 'key':'value' pairs, while lists contains only single elements Examples: dict = {1: "one"; 2: "two", 3: "three"} # a dictionary li = [1,2,3] # a list
14th Jan 2018, 8:06 AM
777
777 - avatar
+ 2
Both are container for a sequence of objects. Lists are orderable, dictionaries are not. Lists use numeric indexes from 0 by step 1 (0,1,2...), dictionaries use keys. Context will determine wich kind of structure is best suited for each cases (but is not limited to these two ones: you could also look at 'tuples' and 'sets' for example, in the built-in sequence data structures ^^)
14th Jan 2018, 9:59 AM
visph
visph - avatar