Dictionaries Code#

Section Title: Dictionaries Code

  • Please solve the following questions using Python code.  

Question-1#

Write a program that constructs a dictionary using two lists provided below, where the keys are taken from loc_list and the values are taken from player_list.

  • Each pair in the dictionary corresponds to a key-value pair with elements from the same index in the lists.

  • Use a for loop for this construction.

loc_list = ['Boston', 'Portland', 'Brooklyn', 'Dallas']
player_list = ['Tatum', 'Lilliard', 'Durant', 'Doncic']

Solution


nba_dict = {}

for i in range(len(loc_list)):
  nba_dict[loc_list[i]] = player_list[i]

print(nba_dict)

Output {‘Boston’: ‘Tatum’, ‘Portland’: ‘Lilliard’, ‘Brooklyn’: ‘Durant’, ‘Dallas’: ‘Doncic’}

Question-2#

Write a program that constructs a dictionary using the given list below, where the keys are taken from state_list and the values represent the number of characters in each state name.

  • Each pair in the dictionary corresponds to a state name and the number of characters in its name.

  • Example: {‘California’, 10}

state_list = ['Utah', 'Nevada', 'Florida', 'Texas', 'Oklahoma', 'Washington', 'Colarado']

Solution-1


for i in range(len(state_list)):
  state_dict[state_list[i]] = len(state_list[i])

print(state_dict)

Output

{‘Utah’: 4, ‘Nevada’: 6, ‘Florida’: 7, ‘Texas’: 5, ‘Oklahoma’: 8, ‘Washington’: 10, ‘Colarado’: 8}

Solution-2

state_dict ={state: len(state) for state in state_list}

print(state_dict)

Output

{‘Utah’: 4, ‘Nevada’: 6, ‘Florida’: 7, ‘Texas’: 5, ‘Oklahoma’: 8, ‘Washington’: 10, ‘Colarado’: 8}

Question-3#

For the given dictionary below, display ‘Close’ values for each day.

stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
              'day-2':{'High': 68, 'Low':60, 'Close':63},
              'day-3':{'High': 71, 'Low':65, 'Close':67},
              'day-4':{'High': 70, 'Low':62, 'Close':65},
              'day-5':{'High': 73, 'Low':65, 'Close':70},
              'day-6':{'High': 75, 'Low':69, 'Close':73}
              }

Solution

for i in stock_dict.values():
  print(i['Close'])

Output

65
63
67
65
70
73

Question-4#

Use the stock_dict to create a list consisting of the ‘Close’ values.”

Solution

close_list = []

for i,j in stock_dict.items():
    close_list.append(  j['Close'])

print(close_list)

Output

[65, 63, 67, 65, 70, 73]

Question-5#

Use stock_dict to construct a dictionary where the keys are days (Day-1, Day-2, …) and the values represent the difference between ‘High’ and ‘Low’ values.

Solution

stock_vol = {}

for i,j in stock_dict.items():
  stock_vol[i] = j['High']-j['Low']

print(stock_vol)

Output

{‘day-1’: 8, ‘day-2’: 8, ‘day-3’: 6, ‘day-4’: 8, ‘day-5’: 8, ‘day-6’: 6}

Question-6#

Use the stock_dict to construct a list consisting of values ‘Increasing’ or ‘Decreasing’ based on the difference between two consecutive day’s ‘Close’ values.

  • Note that there is no ‘Increasing’ or ‘Decreasing’ value for Day-1, as the value before Day-1 is not provided.

Solution

close_diff_list = []

for i in range(len(close_list)-1):
  if close_list[i+1]-close_list[i] > 0:
    close_diff_list.append('Increasing')
  else:
    close_diff_list.append('Deccreasing')

print(close_diff_list)

Output

[‘Deccreasing’, ‘Increasing’, ‘Deccreasing’, ‘Increasing’, ‘Increasing’]

Question-7#

Construct a dictionary using a for loop, where the keys are the names in names_list and the values are the last characters of the corresponding names.

  • Example: key = ashley, value = y

names_list = ['ashley', 'michael', 'jack', 'taylor', 'tim', 'robert', 'joseph']

Solution

last_dict = {}

for i in names_list:
  last_dict[i] = i[-1]

print(last_dict)

Output

{‘ashley’: ‘y’, ‘michael’: ‘l’, ‘jack’: ‘k’, ‘taylor’: ‘r’, ‘tim’: ‘m’, ‘robert’: ‘t’, ‘joseph’: ‘h’}

Question-8#

Construct a dictionary using a for loop, where the keys are the first names in fullnames_list and the values are the corresponding last names.

  • Example: key=’Michael’, value=’Jordan’ for ‘Michael Jordan’ in fullnames_list.

fullnames_list = ['Michael Jordan', 'Larry Bird', 'Jason Tatum', 'Lebron James', 'Jimmy Butler', 'Trae Young', 'Kyrie Irving']

Solution

first_last_name = {}

for i in fullnames_list:
  first_last_name[i.split()[0]] = i.split()[1]

print(first_last_name)

Output

{‘Michael’: ‘Jordan’, ‘Larry’: ‘Bird’, ‘Jason’: ‘Tatum’, ‘Lebron’: ‘James’, ‘Jimmy’: ‘Butler’, ‘Trae’: ‘Young’, ‘Kyrie’: ‘Irving’}

Question-9#

Use a list comprehension to store the last two characters of each name in a list

grades_dict = { 'mike' : {'Math':90, 'History':88, 'Science':73},
                'jack' : {'Math':77, 'History':74, 'Science':52},
                'tim'  : {'Math':98, 'History':35, 'Science':46},
                'liz'  : {'Math':65, 'History':55, 'Science':81},
                'aria' : {'Math':76, 'History':99, 'Science':69}}

Solution

last_two = [i[-2:] for i in grades_dict.keys() ]
print(last_two)

Output

[‘ke’, ‘ck’, ‘im’, ‘iz’, ‘ia’]

Question-10#

Use a for loop to store all Math grades in a list.

Solution

math_list = []

for i in grades_dict.values():
  math_list.append(i['Math'])

print(math_list)

Output

[90, 77, 98, 65, 76]

Question-11#

Use a for loop to find the name of the student with the largest Science grade and print both the name and the corresponding grade.

Solution

largest_name = ''
largest_grade = 0

for i, j in grades_dict.items():
  if j['Science'] > largest_grade:
    largest_name = i
    largest_grade = j['Science']

print(largest_name, largest_grade)

Output

liz 81

Question-12#

Write a function whose parameters are two numbers.

  • It returns a dictionary with

    • keys : sum, difference, product, quotient

    • values: corresponding algebraic results

  • If the second number is zero, then the quotient is labeled as ‘DNE’ (Does Not Exist)

  • Example: inputs 6, 2

    • Output: {'sum': 8, 'difference':4, 'product':12, 'quotient':3.0}

Solution


def operation_func(a, b):
  if b != 0: q =a/b
  else: q = 'DNE'
  return {'sum': a+b, 'difference':a-b, 'product':a*b, 'quotient':q}

print(operation_func(6, 2))
print(operation_func(6, 0))

Output

{‘sum’: 8, ‘difference’: 4, ‘product’: 12, ‘quotient’: 3.0}
{‘sum’: 6, ‘difference’: 6, ‘product’: 0, ‘quotient’: ‘DNE’}