Home > database >  convert tuple to dict and accessing its values
convert tuple to dict and accessing its values

Time:11-19

inputTuple = ({'mobile': '91245555555', 'email': '[email protected]', 'name': 'xyz', 'app_registration': 1},)
print(type(inputTuple))  # <class 'tuple'>

my_dict = dict(inputTuple)
print(my_dict)  #ValueError: dictionary update sequence element #0 has length 4; 2 is required
mobile = my_dict.get("mobile")
email = my_dict.get("email")
name = my_dict.get("name")

print(mobile)
print(email)
print(name)

how to get now each data from this tuple, first how to convert this to dict, i need to convert to dict and have to get all the key pair values,and not by using index values Thanks for the answers

CodePudding user response:

Do you just want

my_dict = inputTuple[0]
data = my_dict['mobile']
print(data) 

CodePudding user response:

inputTuple = ({'mobile': '91245555555', 'email': '[email protected]', 'name': 'xyz', 'app_registration': 1})

In the question the inputTuple value ended with 'comma' which will make it as tuple and if we remove that it will be dict. and it worked in below way after change in question.

 mobile = data.get("mobile", None)

Thanks for all

  • Related