What’s the Difference between a List and a ___? | Python Programming
--
Lists are the backbone of Python programming language and you should know about them.
Python Data Types
The data types common to Python are as follows:
- lists
- dictionaries
- tuples
- sets
- numbers
- strings
- booleans
- and others!
What is the difference between a List and a dictionary?
A dictionary is Python is an un-ordered collection of elements that are accessed by keys. No duplicates are allowed in dictionaries.
A dictionary is made up of key-valued pairs. To access a dictionary’s elements you do so like this:
example_dictionary = {"employee_name":"Justin","employee_id":272}example_dictionary["age"] = 25 // Assign employee's age
Accessing values in a list is different. A list is an ordered collection that is changeable. Duplicate members are allowed. An example list is show below.
example_list = ["Jake","Debbie"]
example_list.append("Ralph")
print(example_list)
print(example_list[0]) // Access list elementResult:
["Jake","Debbie","Ralph"]
"Jake"
Accessing list elements is done using the square brackets ([]) based on numeral indexing.
What is the difference between a List and a set?
A set is a collection of elements that is un-ordered and un-indexed. Sets are also written with curly braces ({}), similar to dictionaries.
Items in a set cannot be accessed by referring to an index. This is because sets are unordered — the items have no indices.
example_set_1 = {"Arkansas","Alabama","Alaska"}
example_set_1.add("California") # Add element to set
example_set_1.update(["Michigan"])# Update a set
However, you can loop through a set using a for loop, as with lists.
# Printing the items in a set
for element in example_set_1…