list dictionary in python code example

Example 1: printing dictionary in python

mydict = {'score1': 41,'score2': 23}
mydict['score3'] = 45

# key value pairs
for i in mydict:
    print(i,mydict[i])

Example 2: python list to dictionary

def to_dictionary(keys, values):
    return dict(zip(keys, values))
    
keys = ["a", "b", "c"]    
values = [2, 3, 4]
print(to_dictionary(keys, values)) 		# {'a': 2, 'c': 4, 'b': 3}

Example 3: 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 4: dictionary in python

# Dictionaries in Python

ages = {"John": 43, "Bob": 24, "Ruth": 76} # Marked by { at beginning and a } at end

# ^^^ Has sets of keys and values, like the 'John' and 43 set. These two values must be seperated by a colon

# ^^^ Sets of values seperated by commas.

Example 5: python dictionary with list

dictionary = {"one": [1, 2, 3, 4, 5], "two": "something"}
print(dictionary["one"][2])


3