Collections and Data Structures

In computer programming, a collection is a grouping of some variable number of data items (possibly zero) that have some shared significance to the problem being solved and need to be operated upon together in some controlled fashion. [1]

This page provides syntax for different types of collections and data structures in Python (arrays, sets, dictionaries, etc.). Each section includes an example to demonstrate the described methods

Arrays

Arrays are ordered collections of elements. In Python they are automatically indexed (consecutively numbered) by an integer starting with 0.

Action
Syntax

New array (empty)

[]

Array with values (integers)

[1, 2, 3, 4, 5]

Array with values (string)

[“a1”, “ab2”, “c3”]

Array of numbers

list(range(1, 11))

Creating Array From String

Action
Syntax

Split string str by delimiter into words (e.g., space)

str.split(“ “)

Accessing Elements

Action
Syntax

Get length of array my_array

len(my_array)

Get first element of array my_array

my_array[0]

Get last element of array my_array

my_array[-1]

Get nth element of array my_array(e.g., 2)

my_array[1]

Check if element is in array

str in my_array

Adding and Removing Elements

Action
Syntax

Add element to end

my_array.append(str)

Remove element from end

my_array.pop()

Remove element from beginning

my_array.pop(0)

Add element to beginning

my_array.insert(0, str)

Sort and Unique

Action
Syntax

Sort array (will not change array itself)

sorted(my_array)

Sort array in place (will change array)

my_array.sort()

Get unique elements in array

list(set(my_array))

Compare Arrays

Action
Syntax

Intersection

set(my_array).intersection(your_array)

Union

set(my_array).union(your_array)

Input:

Output:

Sets

Sets are an unordered collection of unique elements.

Creating Sets

Action
Syntax

New set (empty)

[]

Set with values

my_set = {1, 2, 3, 4, 5}

Set with values

my_set = {"a1", "b2", "c3"}

Interacting With Sets

Action
Syntax

Get length of set my_set

len(my_set)

Check if value is in set

"str" in my_set

Add value

my_set.add("str")

Comparing Sets

Action
Syntax

Intersection

my_set.intersection(your_set)

Union

my_set.union(your_set)

Difference

my_set.difference(your_set)

Input:

Output:

Dictionaries

Dictionaries are unordered collection of key-value pairs where the key serves as the index (“associative collection”). Similar to elements of a set, keys are always unique.

Creating Dictionaries

Action
Syntax

New Dictionary (empty)

{}

Dictionary with values

{"one": 1, "two": 2, "three": 3, "four": 4}

Accessing Dictionaries

Action
Syntax

Get value for key in dictionary my_dict

my_dict["one"]

Check if dictionary has key

"one" in my_dict

Check for key/value pair

("one", 1) in my_dict.items()

Get value and set default

my_dict.get("one", 5)

my_dict.setdefault("five", 5)

Add key/value pair

my_dict["five"] = 5

Delete key/value pair

my_dict.pop("four", None)

Get keys

my_dict.keys()

Get values

my_dict.values()

Converting Dictionaries

Action
Syntax

Convert keys to array

list(my_dict.keys())

Convert values to array

list(my_dict.values())

Sorting Dictionaries

Sorting keys

sorted(my_dict.keys())

Sorting values

sorted(my_dict.values())

Sort by value (descending) with keys

sorted(my_dict.items(), key=lambda x: x[1], reverse=True)

Sort by value (ascending) with keys

sorted(my_dict.items(), key=lambda x: x[1])

Get top n by value (e.g., 3)

sorted(my_dict.items(), key=lambda x: x[1], reverse=True)[:3]

Input:

Output:

References

  1. Wikipedia contributors (n.d.). Collection. In Wikipedia. Retrieved May 1, 2024, from https://en.wikipedia.org/wiki/Collection_(abstract_data_type)

Resources

Last updated