Question

In: Computer Science

Through Python, a dictionary or list is often used to store a sequential collection of elements....

Through Python, a dictionary or list is often used to store a sequential collection of elements. Suppose, for instance, that you need to read 50 numbers, compute their average, and then compare each number with the average to determine whether it is below or above the average. In order to do this (without the use of list), you would first declare 50 variables to store the 50 numbers. This means you would have to create 50 variables and repeatedly write almost the identical code 50 times. Writing a program this way is impractical. To overcome this issue, you can make use of a list to store all the 50 numbers and access them through a single list variable.

For your initial post, describe the difference between dictionary and list in Python and explain when each would be used. Provide an example of how each dictionary and list would be implemented within Python. 250 WORDS

Solutions

Expert Solution

hello,

let us differentiate between both i.e., List and Dictionary:-

List:-

List is a data structure which is defined in to store multiple values within a same variable which is accessed by an index element.

List are more like an array of data but there is no restriction over the type of data stored in a list, as example a list can store any type of data simultaneously like, a list of four element can have values of type integer, float, string, bool.

syntax for defining of a list in python is:-

list_name=[]

example of a list :-

mydata=[1,'Jonney English,3.456,True]               #list shown here have four type of value stores simultaneously

the elements of a list can be accessed by index as example:-

mydata[0]=1

mydata[1]='jonney English'

myadata[2]=3.456

mydata[3]=True

List are mutable which means values stored in list can be changed , as example

mydata[3]=False

now the new list will become:-

mydata=[1,'Jonney English,3.456,False]

Dictionaries:-

dictionaries are also like list but differ in the way that they have keys instead of index .

i.e., they are accessed using user defined keys.

these are also mutable and can be changed/updated.

the elements inside a list may not be arranged in sequential order as they were in list, ( since each element is recognized by key and not by index)

syntax for defining a dictionary:-

dict_name={}

adding elements to dictionary

dict_name['key']=value

accessing elements of dictionary using keys:-

print(dict_name['key'])

example:-

person={}

person['pet']='dog'

person['home']='texas'

person['salary']=$70000

so now the dictionary will look like:-

person={'pet':'dog', 'home':'texas', 'salary':'$70000'}

When should we use any of these:-

List should be used when there is a specific order in which elements are stored

while

dictionary should be used when there is no restriction in order of element and where elements can be accessed using keys.

i hope i was able to solve your problem to a greater extent, please feel free to comment your queries, Please consider my efforts and upvote my solution.

Thanku:)


Related Solutions

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...
The List method addAll(i,c) inserts all elements of the Collection c into the list at position...
The List method addAll(i,c) inserts all elements of the Collection c into the list at position i. (The add(i,x) method is a special case where c = {x}.) Explain why, for the data structures in this chapter, it is not efficient to implement addAll(i,c) by repeated calls to add(i,x). Design and implement a more efficient implementation.
Python Programming Revise the ChatBot program below. There needs to be one list and one dictionary...
Python Programming Revise the ChatBot program below. There needs to be one list and one dictionary for the ChatBot to use. Include a read/write function to the program so that the program can learn at least one thing and store the information in a text document. ChatBot program for revising: # Meet the chatbot Eve print('Hi there! Welcome to the ChatBot station. I am going to ask you a series of questions and all you have to do is answer!')...
USE PYTHON Create a single list that contains the following collection of data in the order...
USE PYTHON Create a single list that contains the following collection of data in the order provided: [1121, "Jackie Grainger", 22.22, 1122, "Jignesh Thrakkar", 25.25, 1127, "Dion Green", 28.75, False, 24.32, 1132, "Jacob Gerber", "Sarah Sanderson", 23.45, 1137, True, "Brandon Heck", 1138, 25.84, True, 1152, "David Toma", 22.65, 23.75, 1157, "Charles King", False, "Jackie Grainger", 1121, 22.22, False, 22.65, 1152, "David Toma"] The data above represents employee information exported from an Excel spreadsheet. Whomever typed the data in originally didn't...
In Python, Given a list of numbers, return a list where all adjacent == elements have...
In Python, Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3,3,2,2,4] returns [1,2,3,2,4]. You may create a new list or modify the passed in list (set function does not work in this case).
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience...
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience on the use of: 1. integers (int) 2. floats (float) 3. conditionals(if statements) 4. iteration(loops) The goal of this project is to make a fictitious comparison of the federal income. You will ask the user to input their taxable income. Use the income brackets given below to calculate the new and old income tax. For the sake of simplicity of the project we will...
CSC202-001 Clark Problem 1: For this class Java’s ArrayList is used to store a collection of...
CSC202-001 Clark Problem 1: For this class Java’s ArrayList is used to store a collection of different elements. Please design and implement your own array-based version of MyArrayList that stores items of type String and supports the following operations: +MyArrayList() //a constructor that creates an empty array list +emptyHere() : Boolean // Determines whether the list is empty or not +sizeHere() : integer // Returns the # of items stored in the array list +add(in item : String) : void...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
python: Implement a function that reverses a list of elements by pushing them onto a stack...
python: Implement a function that reverses a list of elements by pushing them onto a stack in one order, and writing them back to the list in reversed order.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT