Computer Science, asked by BrainlyProgrammer, 4 hours ago

What is the difference between list, tuple and dictionary in python.
Examples?

••Plagiarism/Spams Strictly Prohibited.​

Answers

Answered by shreysaraf1
2

Answer:

List and tuple is an ordered collection of items. Dictionary is unordered collection.

List and dictionary objects are mutable i.e. it is possible to add new item or delete and item from it. Tuple is an immutable object. Addition or deletion operations are not possible on tuple object.

Each of them is a collection of comma-separated items. List items are enclosed in square brackets [], tuple items in round brackets or parentheses (), and dictionary items in curly brackets {}

>>> L1=[12, "Ravi", "B.Com FY", 78.50] #list

>>> T1=(12, "Ravi", "B.Com FY", 78.50)#tuple

>>> D1={"Roll No":12, "class":"B.com FY

List and tuple items are indexed. Slice operator allows item of certain index to be accessed

>>> print (L1[2])

B.Com FY

>>> 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.

>>> print (D1['class'])

B.com FY

>>> print (D1.get('class'))

B.com FY

Example

dictA = {'Mon': '2 pm', 'Tue': '1 pm', 'Fri': '3 pm'}

# Using items()

res = [(k, v) for k, v in dictA.items()]

# Result

print(res)

Output

[('Mon', '2 pm'), ('Tue', '1 pm'), ('Fri', '3 pm')]

Similar questions