1

working with lists, dictionaries, and tuples in python simultaneously

Task

Create a program that generates a list called keys and a list called values. Both lists should have same size n and generate from a random number between 0 to 30. Also, you need to

  • Convert the keys list to tuple tup_keys.
  • Convert the tup_keys and values into dictionary dic_list.
  • Update the value of the dictionary if the value is less than 5 add 10, and decrement by 6 if greater than 19.

Print the following values:

  • Two lists keys and values
  • New Tuple tup_keys
  • New dictionary dict_list
  • Sorted dict_list based on the Keys.
  • The max values in dict_keys
  • The output of the dict_list after updated the values.

Specifications

  • The program inputs are:
    • n is length of the list, ….                          (Must be integer and greater than 20)

Code

from random import randrange
n = 30
keys = []
values = []
#creating keys and values list with size equal to n
for i in range(n):
    num1 = randrange(0,30)
    num2 = randrange(0,30)
    keys.append(num1)
    values.append(num2)
#printing both lists
print("Keys List : ",keys)
print("Values List : ",values)

#converting list to tuples
tup_keys = tuple(keys)
dic_list = {}
#generating dictionary from tuple and value list
for i in range(n):
    dic_list[tup_keys[i]] = values[i]

#printing results
print("Tuple Keys : ",tup_keys)
print("New Dict list : ",dic_list)
print("Sorted dict list(based on keys) :",dict(sorted(dic_list.items(), key=lambda x: x[0])))
print("Max Value from dict key : ",max(dic_list.values()))
#updating values according to conditions given
for k,v in dic_list.items():
    if v < 5:
        v += 10
    elif v > 19:
        v -= 6
    dic_list[k] = v

#printing updated dictionary
print("Updated dict list : ",dic_list)

Output

Keys List :  [20, 6, 22, 4, 4, 28, 17, 23, 29,
              6, 22, 12, 16, 22, 7, 5, 5, 17,
              20, 21, 11, 23, 4, 5, 12, 8, 23,
              23, 24, 27]
Values List :  [9, 24, 3, 22, 23, 16, 6, 0, 14,
                13, 29, 21, 17, 7, 14, 14, 19,
                29, 8, 27, 0, 9, 1, 25, 19, 8,
                1, 4, 0, 21]
Tuple Keys :  (20, 6, 22, 4, 4, 28, 17, 23, 29,
               6, 22, 12, 16, 22, 7, 5, 5, 17, 
               20, 21, 11, 23, 4, 5, 12, 8, 23,
               23, 24, 27)
New Dict list :  {20: 8, 6: 13, 22: 7, 4: 1, 28: 16,
                  17: 29, 23: 4, 29: 14, 12: 19, 16: 17,
                  7: 14, 5: 25, 21: 27, 11: 0, 8: 8,
                  24: 0, 27: 21}
Sorted dict list(based on keys) : {4: 1, 5: 25, 6: 13,
                                   7: 14, 8: 8, 11: 0, 
                                   12: 19, 16: 17, 17: 29,
                                   20: 8, 21: 27, 22: 7,
                                   23: 4, 24: 0, 27: 21,
                                   28: 16, 29: 14}
Max Value from dict key :  29
Updated dict list :  {20: 8, 6: 13, 22: 7,
                      4: 11, 28: 16, 17: 23,
                      23: 14, 29: 14, 12: 19,
                      16: 17, 7: 14, 5: 19,
                      21: 21, 11: 10, 8: 8,
                      24: 10, 27: 15}

keep visiting : shaadcoding

shaad

One Comment

  1. Wow, marvelous blog structure! How long have you ever been blogging for?
    you made blogging look easy. The whole glance
    of your site is wonderful, as smartly as the content material!

Leave a Reply

Your email address will not be published. Required fields are marked *