Computer >> Computer tutorials >  >> Programming >> Python

How to select Python Tuple/Dictionary Values for a given Index?


Items in tuple are indexed. Slice operator allows item of certain index to be accessed

>>> T1=(12, "Ravi", "B.Com FY", 78.50)
>>> print (T1[2])
B.Com FY

Items in dictionary are not indexed. Value associated with a certain key is obtained by putting in square bracket. The get() method of dictionary also returns associated value.

>>> D1={"Rollno":12, "class":"B.com FY", "precentage":78.50}
>>> print (D1['class'])
B.com FY
>>> print (D1.get('class'))
B.com FY