In: Computer Science
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. Actively participate in this discussion by providing constructive feedback on the criteria, rationales, and examples posted by your peers. Provide at least one reference to support your findings.
Difference :
The nearest thing, conceptually, to a Python list is a dynamic array. Elements in a list have the following characteristics
A Python dictionary is an implementation of a hash table. Elements have the following characteristics
Operations in list:
insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position.
remove(a) :- This function is used to delete the first occurrence of number mentioned in its arguments.
del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments.
pop(a) :- This method deletes the element at the position mentioned in its arguments.
Operation in dictionary:
get(key[,d]):Return the value of key. If key doesnot exit, return d (defaults to None).
items():Return a new view of the dictionary's items (key, value).
keys():Return a new view of the dictionary's keys.
pop(key[,d]):Remove the item with key and return its value or d if key is not found. If d is not provided and key is not found, raises KeyError.
popitem():Remove and return an arbitary item (key, value). Raises KeyError if the dictionary is empty.
update([other]):Update the dictionary with the key/value pairs from other, overwriting existing keys.
Explanation:
If you have the keys like :
Student have roll number , name and many other values then you have to use dictionary over lists
if you want to access the date in sequence with the integer index then it is better to use lists over dictionary.
Example:
Student={"name":"John","roll_no":15}// dictionary
list={1,2,5,6,7}