pub-3785248829033902 | Python: Lists, Tuples & Dictionaries | - Big data with Python

| Python: Lists, Tuples & Dictionaries |

Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end.

companies = ['Exxon Mobil', 'Wal-Mart', 'Chevron', 'Apple', 'GM']
=> {'['Exxon Mobil', 'Wal-Mart', 'Chevron', 'Apple', 'GM']}

Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference:

months = ('January','February','March','April','May','June')
=> ('January', 'February', 'March', 'April', 'May', 'June')

Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'.  


The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries. 

The best way to learn new things is to take a practical approach of the things you want to learn. Here is a fragment of code that demonstrates the use of the Dictionary statement in Python:

Æ’ python command

#1
mobile = {'John': 1199, 'Mike': 2244}
mobile['Kate'] = 3377
print(mobile)

#2
#dictionaries from arbitrary key and value expressions
print( {x: x**2 for x in (2, 4, 6)} )

output

#1
{'John': 1199, 'Mike': 2244, 'Kate': 3377}
#2
{2: 4, 4: 16, 6: 36}
  
Previous
Next Post »