In this module, you are working with lists and dictionaries. Consider the follow
ID: 3917252 • Letter: I
Question
In this module, you are working with lists and dictionaries. Consider the following scenario before you make your initial post.
Lists and dictionaries in Python are similar in that they are both used to store a collection of other data. In many cases, a programmer could use either one effectively to achieve a particular goal. Imagine a collection of some sort of data and propose which collection data type, a list or a dictionary, you would choose.
For your initial post, determine whether dictionaries or lists are the most efficient method, and provide a rationale based on what you learned in this module to support your decision.
Explanation / Answer
The Solution - Lists, Tuples, and Dictionaries:
Tuples:
Tuples are pretty easy to make. You give your tuple a name, then after that the list of values it will carry. For example, the months of the year:
Creating a tumple:
Python then organises those values in a handy, numbered index - starting from zero, in the order that you entered them in. It would be organised like this:
And that is tuples! Really easy...
Lists:
Lists are extremely similar to tuples. Lists are modifiable (or 'mutable', as a programmer may say), so their values can be changed. Most of the time we use lists, not tuples, because we want to easily change the values of things if we need to.
Lists are defined very similarly to tuples. Say you have FIVE cats, called Tom, Snappy, Kitty, Jessie and Chester. To put them in a list, you would do this:
Creating a List:
As you see, the code is exactly the same as a tuple, EXCEPT that all the values are put between square brackets, not parentheses. Again, you don't have to have spaces after the comma.
You recall values from lists exactly the same as you do with tuples. For example, to print the name of your 3rd cat you would do this:
Recalling Items from a List:
You can also recall a range of examples, like above, for example - cats[0:2] would recall your 1st and 2nd cats.
Where lists come into their own is how they can be modified. To add a value to a list, you use the 'append()' function. Let's say you got a new cat called Catherine. To add her to the list you'd do this:
Adding Items to a List:
That's a little weird, isn't it? I'll explain. That function is in a funny spot - after a period (the '.' kind of period, not otherwise), after the list name. You'll get to see those things more in a later lesson. For the meanwhile, this is the form of the function that adds a new value to a list
Using the Append function:
Clears things up? Good!
Now to a sad situation - Snappy was shot by a neighbour, and eaten for their dinner (good on 'em!). You need to remove him (or her) from the list. Removing that sorry cat is an easy task, thankfully, so you have to wallow in sadness for as short a time as possible:
Deleting an item:
You've just removed the 2nd cat in your list - poor old Snappy.
Dictionaries:
Ok, so there is more to life than the names of your cats. You need to call your sister, mother, son, the fruit man, and anyone else who needs to know that their favourite cat is dead. For that you need a telephone book.
Now, the lists we've used above aren't really suitable for a telephone book. You need to know a number based on someone's name - not the other way around, like what we did with the cats. In the examples of months and cats, we gave the computer a number, and it gave us a name. This time we want to give the computer a name, and it give us a number. For this we need Dictionaries.
So how do we make a dictionary? Put away your binding equipment, it isn't that advanced.
Remember, dictionaries have keys, and values. In a phone book, you have people's names, then their numbers. See a similarity?
When you initially create a dictionary, it is very much like making a tuple or list. Tuples have ( and ) things, lists have [ and ] things. Guess what! dictionaries have { and } things - curly braces. Here is an example below, showing a dictionary with four phone numbers in it:
Creating a Dictionary:
the program would then print Lewis Lame's number onscreen. Notice how instead of identifying the value by a number, like in the cats and months examples, we identify the value, using another value - in this case the person's name.
Ok, you've created a new phone book. Now you want to add new numbers to the book. What do you do? A very simple line of code:
Adding entries to a dictionary:
All that line is saying is that there is a person called Gingerbread Man in the phone book, and his number is 1234567. In other words - the key is 'Gingerbread Man', and the value is 1234567.
You delete entries in a dictionary just like in a list. Let's say Andrew Parson is your neighbour, and shot your cat. You never want to talk to him again, and therefore don't need his number. Just like in a list, you'd do this:
Removing entries from a dictionary:
Again, very easy. the 'del' operator deletes any function, variable, or entry in a list or dictionary (An entry in a dictionary is just a variable with a number or text string as a name.
Python Dictionary Methods
Python has some methods that dictionary objects can call. For example, dict1.clear()method removes all items from the dictionary dict1.
Here i includes all dictionary methods available in Python 3. Also, Includes built-in functions that can take dictionary as a parameter and perform some task. For example, the sort() function can take dictionary as a parameter and sort it.
METHOD DESCRIPTION
1.Python Dictionary clear() >>> Removes all Items Remove all items
2.Python Dictionary copy() >>> Returns Shallow Copy of a Dictionary
3.Python Dictionary fromkeys() >>> Creates Dictionary From Given Sequence
4.Python Dictionary get() >>>Returns Value of The Key
5.Python Dictionary items() >>>Returns Riew Of Dictionary's (Key, Value) Pair
6.Python Dictionary keys() >>>Returns View Object of All Keys
7.Python Dictionary popitem() >>>Returns & Removes Element From Dictionary
8.Python Dictionary setdefault() >>>Inserts Key With a Value if Key is not Present
9.Python Dictionary pop() >>>removes and returns element having given key
10.Python Dictionary values() >>>returns view of all values in dictionary
11.Python Dictionary update() >>>Updates the Dictionary
12.Python any() >>>Checks if any Element of an Iterable is True
13.Python all() >>>returns true when all elements in iterable is true
14.Python ascii() >>>Returns String Containing Printable Representation
15.Python bool() >>>Coverts a Value to Boolean
16.Python dict() >>>Creates a Dictionary
17.Python enumerates() >>>Returns an Enumerate Object
18.Python filter() >>>constructs iterator from elements which are true
19.Python iter() >>>returns iterator for an object
20.Python len() >>>Returns Length of an Object
21.Python max() >>>returns largest element
22.Python min() >>>returns smallest element
23.Python map() >>>Applies Function and Returns a List
24.Python sorted() >>>returns sorted list from a given iterable
25.Python sum() >>>Add items of an Iterable
26.Python zip() >>>Returns an Iterator of Tuples
Index Value 0 January 1 Feb 2 Mar 3 April 4 May 5 June 6 July 7 Aug 8 Sept 9 Oct 10 Nov 11 DecmRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.