Question

The list and dictionary object types are two of the most important and often used types...

The list and dictionary object types are two of the most important and often used types in a Python program. Compare the functionalities of those two objects. What are some ways to insert, update, and remove elements from lists and dictionaries? Why would you choose one data type over another? Provide code examples demonstrating the usages of both data types.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Both list and dictionary are linear data types...

lets see first : list

elements are accessed in list through the index..

example:

l = [1,6,4,5]

to access 3rd element we use   l[3]

here 3 is index value...of 4

to update in list

just simply use

l[3] =10

now

the list will be

l = [1,6,10,5]

to insert element to list:

to insert at position 3

then l.insert(3,20)

at position 3 inserting 20

list will be

l = [1,6,10,20,5]

to delete element from list :

l.remove(6)

removes the element 6 from list

l = [1,10,20,5]

lets see dictionary:

values...are accesed through keys in dictionaries

example:

d = {'a': 1, 'b': 'a' }

d['a'] will be 1

here keys and values can be any thing char,string,int float...

data can be accessed through any data type...

this is the advantage of dictionary over list...in list data accessed by int data type only..

you want to use strings /chars as ur indexes then use dictionary

updating a value

d['a'] =2

now dictionary

d = {'a': 2, 'b': 'a' }

inserting a value to dictionary:

d['c']=3

adding new item

deleting from dictionary

just use

del d['a']

Add a comment
Know the answer?
Add Answer to:
The list and dictionary object types are two of the most important and often used types...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT