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 are ordered collections of elements. In Python they are automatically indexed (consecutively numbered) by an integer starting with 0.
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Input:
Output:
Sets are an unordered collection of unique elements.
Input:
Output:
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.
Input:
Output:
Wikipedia contributors (n.d.). Collection. In Wikipedia. Retrieved May 1, 2024, from https://en.wikipedia.org/wiki/Collection_(abstract_data_type)
W3 Schools: Python Data Structures
Data Quest: Python Data Structures
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
Action | Syntax |
---|---|
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))
Split string str by delimiter into words (e.g., space)
str.split(“ “)
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
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 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))
Intersection
set(my_array).intersection(your_array)
Union
set(my_array).union(your_array)
New set (empty)
[]
Set with values
my_set = {1, 2, 3, 4, 5}
Set with values
my_set = {"a1", "b2", "c3"}
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")
Intersection
my_set.intersection(your_set)
Union
my_set.union(your_set)
Difference
my_set.difference(your_set)
New Dictionary (empty)
{}
Dictionary with values
{"one": 1, "two": 2, "three": 3, "four": 4}
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()
Convert keys to array
list(my_dict.keys())
Convert values to array
list(my_dict.values())
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]