python dict list code example
Example 1: python dict
my_dict = {
'spam': 'eggs',
'foo': 4,
100: 'bar',
2: 0.5
}
print(my_dict['spam'])
print(my_dict['foo'])
print(my_dict[100])
print(my_dict[2])
for key, value in my_dict.items():
print(key, value)
print(len(my_dict))
my_dict['baz'] = 'qux'
my_dict['baz'] = 'quxx'
del my_dict['spam']
print(my_dict.copy())
print(my_dict.fromkeys('added', 100))
print(my_dict.get('foo'))
print(my_dict.items())
print(my_dict.keys())
print(my_dict.values())
my_dict.setdefault('a', 'b')
my_dict.pop('foo')
my_dict.popitem()
my_dict.update({'baz': 'val'})
my_dict.clear()
Example 2: python list of dictionaries
new_player1 = { 'firstName': 'LaMarcus', 'lastName': 'Aldridge', 'jersey': '12', 'heightMeters': '2.11', 'nbaDebutYear': '2006', 'weightKilograms': '117.9'}
new_player2 = { 'firstName': 'LeBron', 'lastName': 'James', 'jersey': '2', 'heightMeters': '2.03', 'nbaDebutYear': '2003', 'weightKilograms': '113.4' }
new_player3 = { 'firstName': 'Kawhi', 'lastName': 'Leonard', 'jersey': '2', 'heightMeters': '2.01', 'nbaDebutYear': '2011', 'weightKilograms': '104.3' }
nba_players = []
nba_players.append(player)
nba_players.append(new_player1)
nba_players.append(new_player2)
nba_players.append(new_player3)
Example 3: python dictionary with list
dictionary = {"one": [1, 2, 3, 4, 5], "two": "something"}
print(dictionary["one"][2])
3