Skip to content

Basic Collections

Theo Chupp edited this page Feb 4, 2018 · 2 revisions

Collections

Collections are a basic data structure in Python. Simply, they are a one-dimensional collection of variables containing 0 to infinite values.

There are a few variations:

The items in a list are called elements

Tuples

A tuple is the most basic collection. They are very useful if you want to specify a collection that you don't want modified later.

im·mu·ta·ble
adjective
unchanging over time or unable to be changed.
"an immutable fact"
synonyms: fixed, set, rigid, inflexible, permanent, established, carved in stone;

Tuples are immutable

They are defined by writing out values separated by commas

# Tuple with 3 elements
nums = (1, 2, 3)
print nums # prints (1, 2, 3)

# Also valid, parentheses are optional
nums = 1, 2, 3
print nums # prints (1, 2, 3)

# Tuple with 1 element
nums = 1,
print nums # prints (1,)

We can put any sort of type in a collection, even mixing them if we're feeling funky

# Tuple of strings
pets = ("dog", "cat", "tiger")

# Tuple with a string, a float, and another tuple
numbers = ("one", 3.3, (4, "five"))

In an algebraic sequence, an element is referred to by its term number
In a collection, an element is referred to by its index
If a collection is thought of as an algebraic function, the indexes are the domain, and the elements are the range

The elements of a collection may be accessed by their index
In programming, the first index is 0 (starting from the left)

nums = 9, 25, 49
print nums[0] # prints 9
print nums[2] # prints 49

We can use a negative index to access the list from the right side

nums = 9, 25, 49
print nums[-1] # prints 49
print nums[-3] # prints 9

If we try to access an index that is past the end of the list, an error is returned

nums = 1, 2, 3, 4

print nums[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: tuple index out of range

print nums[-5]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: tuple index out of range

We can find the length of any collection by using the built-in len function

nums = 5, 9
print len(nums) # prints 2

# We can use len(nums) as a variable
nums[len(nums) - 1] # prints 9

Some mathematical operations work on collections

a = 1, 3
b = 2, 4
print a + b # prints (1, 3, 2, 4)

print a * 3 # prints (1, 3, 1, 3, 1, 3)

We can use slicing to retrieve part of a collection based on the indexes

nums = 1, 2, 3, 4

# The first value is inclusive, and the second value is exclusive
# nums[0:2] will return the 0th and 1st index
# In algebraic terms, nums[0:2] limits the domain of 'nums' to (0, 2]
print nums[0:2] # prints (1, 2)
print nums[1:4] # prints (2, 3, 4)

# You may skip values while slicing by providing an increment as the 3rd argument
print nums[0:4:2] # prints (1, 3)

# You may reverse a collection by specifying a negative increment
print nums[0:4:-1] # prints (4, 3, 2, 1)

# Slicing has defaults if an input is not specified
# nums[:2] defaults the first value to the beginning of the collection
print nums[:2] # prints (1, 2)
# nums[2:] defaults the second value to the end of the collection
print nums[2:] # prints (3, 4)

print nums[:] # prints (1, 2, 3, 4)

We can ask a collection if it contains an element with the in operation

nums = 1, 2, 3, 4

print 4 in nums # prints True
print 8 in nums # prints False

Lists

Lists have all the same properties as Tuples, with additional features.

# List with 4 elements
nums = [1, 2, 3, 4]
print nums # prints [1, 2, 3, 4]

List have an additional feature, we can change the values of a list.

nums = [1, 2, 3, 4]
print nums # prints [1, 2, 3, 4]

nums[2] = 5
print nums # prints [1, 2, 5, 4]

Lists also allow us to add elements using the append method
Methods are functions that are invoked on objects

nums = [1, 2]
print nums # prints [1, 2]

nums.append(4)
print nums # prints [1, 2, 4]

Sets

Sets are a type of collection that can only contain unique elements

nums = set([1, 2, 3, 3])
print nums # prints set([1, 2, 3])

# Alternative declaration
nums = {1, 2, 3, 3}
print nums # prints set([1, 2, 3])

Unlike Tuples and Lists, Sets are not guaranteed to maintain order

nums = {3, 5, 1, 8}
print nums # prints set([8, 1, 3, 5])

You may add elements to a set, but unlike Lists, the method is called 'add'

nums = {3, 5, 1, 8}
print nums # prints set([8, 1, 3, 5])

nums.add(4)
print nums # prints set([8, 1, 3, 4, 5])

nums.add(10)
print nums # prints set([1, 3, 4, 5, 8, 10])

Reference

https://codeburst.io/python-basics-6-lists-and-list-manipulation-a56be62b1f95

Clone this wiki locally