rurtubia
5/29/2015 - 4:23 AM

Dictionaries or lists in Python

Dictionaries or lists in Python

# It is made up of values with a unique key for each of them.

# It is not possible to join dictionaries the way lists can be joined.

# Example: military alphabet

military_alphabet = {'A':'alpha', 'B': 'bravo', 'C': 'charlie', 'D':'delta', 'E':'echo', 'F':'foxtrot', 'G':'golf',
                     'H':'hotel', 'I':'india', 'J':'juliet', 'K':'kilo', 'L':'lima', 'M':'mike', 'N':'november',
                     'O':'oscar', 'P':'papa', 'Q':'quebec', 'R':'romeo', 'S':'sierra', 'T':'tango', 'U':'uniform',
                     'V':'victor', 'W':'whiskey', 'X':'x-ray', 'Y':'yankee', 'Z':'zulu'}

# Get one item from the dictionary
print(military_alphabet['A'])

# Delete an entry
del military_alphabet['A']

# Replace an item in the dictionary
military_alphabet['R'] = 'rurtubia'

#Find the number of items or length for the dicionary
len(military_alphabet)
print('There are ',len(military_alphabet),'letters in the English alphabet... because I just eliminated one!')

#Find the value of an index by passing its key
print("The value of \'X\' is", end=' ')
print(military_alphabet.get("X"))

#Obtain a list of the keys in the dictionary
military_alphabet.keys()
print('\n',military_alphabet.keys())

#Obtain a list of the values in the dictionary
military_alphabet.values()
print(military_alphabet.values())