Basic Data Structures in Python

Basic Data Structures in Python

Overview

This article will explain what is a data structure, the differences and how to use commonly used data structures in Python: lists, tuples, sets and dictionaries.

Materials or Technology Needed

  • Computer

  • Python

  • Command Prompt, Terminal or IDE

What is a Data Structure?

A data structure is writing code that can hold, access and use data in different ways. A variable can be assigned to hold a value, but there are more complex implementations we call data structures to hold many values differently that can be called upon.

Different Examples of Data Structures

Common types of data structures (1) are:

  • Lists

  • Tuples

  • Sets

  • Dictionaries

How to Declare These Data Structures

Lists

To create a list, assign a variable a name and set it equal to a bracket or [], as shown below:

make_a_list = ['element1', 1, 'element2']

Tuples

To create a tuple, assign a variable a name and set it equal to a parenthesis or (), as shown below:

make_a_tuple = ('element1', 1, 'element2')

Sets

To create a set, assign a variable a name and set it equal to a bracket or {}, as shown below:

make_a_set = {'element1', 1, 'element2'}

Dictionaries

To create a dictionary, assign a variable a name and set it equal to a bracket or {}. It is similar to a set, but dictionaries use key-value pairs which are assigned in this format ' "key_name":value ' as shown below:

make_a_dictionary = {"key_name": value1, "key_name2": value2}

Every variable or data type within the list must be separated by a comma.

Differences between each of the four Data Structures

A list, tuple, set and dictionary all have slight differences.

A list contains elements in the order it was given though it can be modified, and there can be duplicate variables or numbers. As shown below, the number 2 is included twice in the list.

another_list = [1,4,5,2,2]
print(another_list)
#output:
#[1, 4, 5, 2, 2]

A tuple is similar to a list where it contains elements in the order it was given. The only difference is tuples after being declared cannot be modified. If a variable was going to be reassigned in one of another_tuple's indexes such as "another_tuple[0] = 5", it would result in an error since tuples cannot be modified after being declared. As shown below, a tuple allows duplicates as the number 3 is included twice in the list.

another_tuple = (1,2,3,3)
print(another_tuple)
#output:
#(1, 2, 3, 3)

A set contains one of each element, it cannot be modified after a declaration like tuples and every element are in numerical order. Other elements are not in order:

make_a_set = {1, 4, 3, 3, "dog", "cat"}
print(make_a_set)
#output
#{1, 3, 4, "dog", "cat"} or {1, "dog", 3, 4, "cat"} or {1, 3, "cat", "dog", 4}, etc

All the numerical elements are printed in order and every string is printed in a random index that is not in alphabetical order. There are also no duplicate elements as there are two "3" but only one 3 is printed out.

A dictionary contains key-value pairs. A dictionary can have either a string or number in either of the key or value spaces in the dictionary. Each key value cannot be repeated more than once, and every key has a value. Values can be repeated.

another_dictionary = {"key1": 3, "key3": 5, "key2": 3,  "key3": "hello", 7:"seven"}
print(another_dictionary) 
#output:
#{'key1': 3, 'key3': 'hello', 'key2': 3, 7: 'seven'}

As shown above, "key3" has been written twice, but the output will only show it has "hello" since that was the latest entry of key3 in the dictionary. It still prints the latest updated value for key3 at index1 since that was where key3 first appeared.

In a list, tuple, set or dictionary, commonly strings, numbers as well as other data structures shown below can be included.

Data Structures that contain Data Structures

Below shows a few examples of how a data structure can also contain other data structures as well:

A List that Contains a Tuple, Set and Dictionary

list2 = [(1, 2), {5}, {"key1": "hey"}]
print(list2)
#output
#[(1, 2), {5}, {'key1': 'hey'}]

A Tuple that Contains a List, Set, and Dictionary

tuple2 = ([1, 5], {100}, {"key1": 5, "key2":7})
print(tuple2)
#output
#([1, 5], {100}, {'key1': 5, 'key2': 7})

A Set that Contains a List, Tuple and Dictionary

set2 = {[1,7], (7, 8, 9), {"key1": 15} }
print(set2)
#output
#([1, 5], {100}, {'key1': 5, 'key2': 7})

A Dictionary that Contains a List, Tuple and Set

dictionary2 = {"key1": [1, 2, 3], "key2": (1, 3, 5), "key3": {10}}
print(dictionary2)
#output
#{'key1': [1, 2, 3], 'key2': (1, 3, 5), 'key3': {10}}

Removing Elements

Elements in either a list, tuple, set or dictionary can be removed by using the del command. del stands for delete. To use del, specify the data structure and indexes to remove elements in those positions. If no indexes are specified, then the entire variable is deleted.

list3 = [4, 5, 6, 7, 8]
del list3[2]
print(list3)
#output
#[4, 5, 7, 8]

Additional Commands or Built-in Methods for the List Data Structure

There are a few built-in methods to modify lists:

Append

Append adds an element to an existing list at the last index. Append only takes one element at a time.

list3 = [1,5]
list3.append(3)
print(list3)

#output
#[1, 5, 3]

Extend

Extend takes one list's data and combines it into another list. Two lists must exist for this command to work.

list3 = [1,5]
list4 = [2,7]

list3.extend(list4)

print(list3)
print(list4)

#outputs:
#[1, 5, 2, 7]
#[2, 7]

The output above shows, list3 was only updated while list4 remains the same. As stated, extend takes one list's data and combines it into another list. The first list specified in the extend command will be updated while the other list will not be.

Insert

Insert takes two parameters, the first parameter is at what index you want the element to be added and the second parameter is the variable or data that is being added

list3 = [7, 1, 5]
list3.insert(1,6)
print(list3)
#output
#[7, 6, 1, 5]

The command list3.insert(1,6) at index one for list3, the number 6 will be added. The previous existing element at index one and every other element after index one, is shifted one place to the right since one element is being added at index one.

Remove

Remove disposes of the element that is specified. If one or more of the same element exists in the list, it removes the specified element that first appears in the lowest index.

list3 = [7, 1, 5, 6, 5, 5]
list3.remove(5)
print(list3)
#output 
#[7, 1, 6, 5, 5]

list3.remove(5)
print(list3)
#final output
#[7, 1, 6, 5]

The first .remove will dispose of the 5 at index two since that is the lowest index where 5 first appears. The new list will be updated as [7, 1, 5, 6, 5] and using the remove command will dispose of the next 5 at the lowest index which is three. The new list after using remove twice will be [7, 1, 6, 5].

Pop

Pop removes an element from the list. The number or parameter that is given to pop will remove that element from that index.

list3 = [7, 1, 8, 6, 5, 9]
list3.pop(2) 
# At index two, the element 8 will be removed

If no parameter is given, then pop removes the element at the last index of the list.

Clear

Clear removes every element in the list. Afterward, the list becomes an empty list or [].

list3 = [7, 1, 8, 6, 5, 9]
list3.clear()
print(list3)
#output
#[]

Index

Index takes in a string or number, finds and prints out only the lowest index of that specified string or number where it was found.

list3 = [3, 8, 1, 6, 1]
print(list3.index(1))
#output
#2

Though "1" appears at index two and index four, it will only return where 1 is found at the lowest index, which is 2.

Count

Count returns how many times an element appears in the list.

list3 = [3, 8, 1, 1, 6, 5, 1]
print(list3.count(1))
#output
#3

Sort

Sort sorts the list elements in alphabetical or numerical order

list3 = [3, 8, 1, 1, 6, 5, 1]
list3.sort()
print(list3)
#output
#[1, 1, 1, 3, 5, 6, 8]

Reverse

Reverse prints the entire list backward. The first element in the original list is found in the last index, the second element is now found second to the last index and so on.

list3 = [3, 8, 1, 1, 6, 5, 1]
list3.reverse()
print(list3)
#output
#[1, 5, 6, 1, 1, 8, 3]

Copy

Copy makes a new list, a duplicate of an old list. In this case, list4 copied the contents from list3.

list3 = [3, 8, 1, 1, 6, 5, 1]
list4 = []

list4 = list3.copy()
print(list4)

#output
#[3, 8, 1, 1, 6, 5, 1]

Sources

https://docs.python.org/3/tutorial/datastructures.html (1)