Python Sequence Basics

Python official docs on:

Python email listserv - What is the square bracket about? https://mail.python.org/pipermail/tutor/2006-July/048151.html

Strings

References:

Tuples

Checking the length of a tuple

Accessing a member of a tuple

This is just like accessing the first character of a string:

The square brackets in Python is an operator used to specify an index of a collection.

The following indexing operation returns the element in position 2 of mytuple:

>>> mytuple = ('a', 'b', 'c', 'd')
>>> mytuple[2]
>>> 'c'

Python sequences start at position 0. Thus, to reference the element at what we humans consider the nth position, e.g. 3rd, we use the index value of n - 1, e.g. 2.

Accessing the first member of a tuple

>>> mytuple = (1, 2, 3)
>>> mytuple[0]
1

Accessing the last member of a tuple

>>> mytuple = (1, 2, 3)
>>> mytuple[-1]
3

Slicing a tuple

>>> mytuple = (1, 2, 3, 4)
>>> mytuple[1:3]
(2, 3)

Creating an empty tuple

Since tuples are denoted by parentheses, a set of parentheses with nothing inside will be interpreted as a tuple:

>>> mytuple = ()
>>> type(mytuple)
>>> type(mytuple)
tuple
>>> mytuple
()

Create a tuple of 1 element

This is where things get a little unexpected:

>>> mytuple = (1, )
>>> type(mytuple)
tuple
>>> mytuple
(1,)
>>> len(mytuple)
1

That trailing comma is absolutely necessary in order to denote a one-element tuple.

The following snippet will result in a integer, not a tuple, because the Python interprets (1) as a value enclosed in parentheses:

>>> mytuple = (1)
>>> type(mytuple)
int
>>> mytuple
1

Create an empty tuple using the tuple() class function

tuple, when invoked as a function (i.e. using parentheses) will create a new tuple object. If no arguments are passed in, the result tuple is empty:

>>> t = tuple()
>>> type(t)
tuple
>>> t
()

Convert an iterable object into a tuple

An iterable object is any kind of sequence. This includes strings, which are sequences of characters. To convert a Python string into a tuple in which each element of the tuple is a separate character, use the tuple() class function and pass in a string:

source

List

The Python list is an ordered sequence of elements. In code, a list is denoted with square brackets:

mylist = ['hello', 'world', 42]

Accessing elements of a list is similar to a tuple: use square bracket

similar to a tuple. And like a tuple, a list’s elements are accessible

list’s elements are accessed by referring to their index number. Python lists are denoted with square brackets.

Besides the square brackets, what’s the difference between a list and a tuple? The fact that a list is mutable, i.e changeable. We can add items to a list:

>>> a_list = [1, 2]
>>> a_list.append('hi')
>>> a_list
[1, 2, 'hi']

We can remove items:

>>> b_list = [1, 2, 3]
>>> b_list.pop()
3
>>> b_list
[1, 2]

And more commonly, we can change items:

>>> c_list = [1, 2]
>>> c_list[0] = 99
>>> c_list
[99, 2]

Create a new, empty list

List objects are denoted with square brackets:

mylist = []

Using the list() class function to create a new list

mylist = list()

Convert an iterable object into a new list object

Pass an iterable object as argument into the list() function. For example a Python string is iterable, in that Python treats strings as a collection of individual characters:

>>> txt = 'hello'
>>> z = list(txt)
>>> z
['h', 'e', 'l', 'l', 'o']

Convert a tuple into a new list

Passing a tuple as argument into list() will return a new list containing the same values as the tuple:

>>> t = ('hello', 'world', 42)
>>> list(t)
['hello', 'world', 42]

Create a list from a dictionary’s keys

Note

note

This recipe assumes you know what a Python dictionary is. If not, you’ll read about it son enough.

Pass a dictionary as an argument into list(). The return value will be a list containing the dictionary’s keys:

>>> d = {'oranges': 20, 'apples': 42}
>>> list(d)
['apples', 'oranges']

Note one very important thing about dictionaries: do not assume that their key/value pairs come in order. For example, you should not have expected the resulting list to be:

['oranges', 'apples']

Create a list of a dictionary’s key-value pairs

Dictionaries have the items method which returns a sequence of the key-value pairs as 2-item sequences – basically, a list of 2-element-sequences:

>>> d = {'oranges': 20, 'apples': 42}
>>> list(d.items())
[('apples', 42), ('oranges', 20)]
References:

Ranges