What’s the Difference between a List and a ___? | Python Programming

Justin Alvey
3 min readJul 3, 2020

Lists are the backbone of Python programming language and you should know about them.

Photo by Sean Pierce on Unsplash

Python Data Types

The data types common to Python are as follows:

  1. lists
  2. dictionaries
  3. tuples
  4. sets
  5. numbers
  6. strings
  7. booleans
  8. 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 element
Result:
["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…
Justin Alvey

I’m passionate about #entrepreneurship, #programming, #space, #technology, and #writing.