Question

In: Computer Science

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

Solutions

Expert Solution

Difference :

The nearest thing, conceptually, to a Python list is a dynamic array. Elements in a list have the following characteristics

  • They maintain their ordering unless explicitly re-ordered (for example, by sorting the list).
  • They can be of any type, and types can be mixed.
  • They are accessed via numeric (zero based) indices.

A Python dictionary is an implementation of a hash table. Elements have the following characteristics

  • Every entry has a key and a value
  • Ordering is not guaranteed
  • Elements are accessed using key values
  • Key values can be of any hashable type (i.e. not a dict) and types can be mixed
  • Values can be of any type (including other dict's), and types can be mixed

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}


Related Solutions

Regression and Correlation are two of the most often used and abused tools in research. People...
Regression and Correlation are two of the most often used and abused tools in research. People are quick to jump to conclusions that if a relationship (correlation) exists between two variables, then one must cause (causation) the other. There are many reasons why two variables can be related without causality. Please respond to the following: Comparing the amount of money people spend and the amount people save, your analysis revealed an R-squared = 0.97. Should you use this for predictive...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary looks like this: {‘US’: [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)}, {'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)}, {'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)}, {'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)}, {'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)}, {'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}], 'Australia'...
which contract is most often used in the medical office?
which contract is most often used in the medical office?
List and describe two concepts that you feel were most important to you in this course....
List and describe two concepts that you feel were most important to you in this course. Why were these important to you?
1.List three important things that kidneys do to REGULATE your blood chemistry 2.List two types of...
1.List three important things that kidneys do to REGULATE your blood chemistry 2.List two types of molecules or cells that are retained by the glomerulus. Explain why these items are NOT filtered out of the blood. 3.Describe how urea is used in the loop of the nephron to create the countercurrent multiplier system. What other benefit does this have for the body
List two types of costing systems, and list three types of companies that use one or...
List two types of costing systems, and list three types of companies that use one or the other of the methods.
31)Describe what type of pain Opioids are most often used for. 32)Methadone is often given to...
31)Describe what type of pain Opioids are most often used for. 32)Methadone is often given to patients with an opioid addiction. What type of EKG changes can methadone cause? 33)Review what the best type of pain medication is used in neuropathic pain. 34)Review the best pain medication to use with persistent, severe pain in patients already opioid tolerant 35)Review why using a continued-release preparation such as Oxycontin may give more continuous relief then giving the medication prn. Do patients become...
Procedure: For each object, list the method for determining cosmic distances that would provide the most...
Procedure: For each object, list the method for determining cosmic distances that would provide the most accurate measure of the distance. Explain why you chose the method you did. 1. The Sun 2. Venus 3. Faintly visible red dwarf main-sequence star 4. Moderately bright spectral type F1 main-sequence star 5. Globular cluster orbiting the Milky Way 6. Irregular galaxy in the Local Group 7. Galaxy at the far edge of the Virgo Cluster 8. Large spiral galaxy in a distant...
The parameter most often used to characterize the response of a catchment to a rainfall event...
The parameter most often used to characterize the response of a catchment to a rainfall event is the time of concentration. 1 . What are the two commonly accepted definitions of the time of concentration? 2. Describe the various methods used to estimate the time of concentration.
Python please A string is one of most powerful data types in programming. A string object...
Python please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in IndexError exception. In Python a string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT