Please enable JavaScript.
Coggle requires JavaScript to display documents.
Collections (in-built) in Python - Coggle Diagram
Collections (in-built) in Python
List
Ordered
it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
Changeable
we can change, add, and remove items in a list after it has been created
Allow Duplicates
Since lists are indexed, lists can have items with the same value
mylist = ["apple", "banana", "cherry"]
The list() Constructor
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
Using the list() constructor to make a List:
tuple
Ordered
UnChangeable
Allow Duplicates
The tuple() Constructor
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
Using the tuple() method to make a tuple:
mytuple= ("apple", "banana", "cherry")
Set
Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
Unchangeable
Sets are unchangeable, meaning that we cannot change the items after the set has been created.
Once a set is created, you cannot change its items, but you can add new items.
Duplicates Not Allowed
myset = {"apple", "banana", "cherry"}
Dictionary
A dictionary is a collection which is unordered, changeable and does not allow duplicates.
Dictionaries are written with curly brackets, and have keys and values:
Dictionaries are used to store data values in key:value pairs.
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964}
print(thisdict)
Print the "brand" value of the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964}
print(thisdict["brand"])
Unordered
Changeable
Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created
Duplicates Not Allowed
Dictionaries cannot have two items with the same key
Duplicate values will overwrite existing values:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2012}
print(thisdict)