Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Python Data Types

Sequences

  • Sequences are all indexed by numbers.
  • There are six sequence types:
    • Strings
    • Byte sequences (bytes objects)
    • bytearray
    • lists
    • tuples
    • range

List

  • A specific type of sequence (collection indexed by a number).
  • Muteable (can be edited after creation)
  • Elements can be of different types.

Example construction

emptyList = []
emptyList2 = list()

listOfDifferentTypes = ['a', 1]

// The following two result in exactly the same list
list1 = list('abc')
list2 = ['a', 'b', 'c']

// Lists can contain lists
listOfLists = [['abc'], ['def'], ['ghi']]

Tuples

  • A specific type of sequence (collection indexed by a number).
  • Immutable (once created they cannot be changed).
  • Elements within be of different types (including other tuples).

Example Construction

emptyTuple1 = ()
emptyTuple2 = tuple()

// The following tuples are the same
tuple1 = 12345, 54321, 'hello!'
tuple2 = (12345, 54321, 'hello!')
tuple3 = tuple([12345, 54321, 'hello!'])

// The following tuples are the same
tuple4 = tuple('abc')
tuple5 = tuple(['a', 'b', 'c'])

Range

  • A specific type of sequence (collection indexed by a number).
  • Immutable
  • Always numbers
  • Commonly used in for loops
  • This is an object that has an iteratable interface, not a function call to create a sequence of numbers and store them. Hence it is memory efficient.

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

Example Creation

range1 = range(0, 10, 1)
range1 = range(0, 20, 2)

Dictionaries

  • A collection (not sequence) that is indexed by keys
  • The keys:
    • Must be unique.
    • have to be an immutable type.
      • strings
      • numbers
      • tuples that contain only strings, numbers, and tuples
  • Not immutable (can be altered after creation)
  • Closest type to PHP's associative array. A hash table.

Example Creation

tel = {'jack': 4098, 'sape': 4139}
tel = dict([('jack', 4098), ('sape', 4139)])

Set

  • An unordered collection.
  • Every element within the collection must be unique.
  • As sets are not a sequence, they do not support indexing, slicing, or other sequence-like behaviour.

Example Creation

Can be created in two ways:

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
a = set('abracadabra')

You may have noticed that the examples above had duplicate elements in creation. This will not throw an error, but the set will be created with only the unique items.

Checking Variable Type

If you are ever unsure if a variable is a list, dictionary, set etc, just use __class__ as shown below

listOfLists = [['abc'], ['def'], ['ghi']]
listOfLists.__class__
// outputs <class 'list'>

References

Last updated: 20th April 2021
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch