Sept. 1, 2020

Create or declare new Python lists

Declare an empty list, or generate a flat or nested list with sequence of elements from ranges, strings, dictionary, Numpy arrays or other Python iterables.


Page contents

Initialize empty list

Primary syntax for declaring a new list, from any iterable object.

Py3: Syntax

list( iterable=() ) --> new list

Follow any following notation to create an empty Python list: * explicitly using the built-in list() function, which is more readable * applying the short-hand square bracket notation: [ ]

Py3: Declare empty list object

#   create an empty list
empty_list = list()             #= []
empty_list = []                 #= []

The following expressions all evaluate to empty Python lists.

Py3: Evaluating to empty lists

#   explicitly converting empty iterables into empty lists
empty_list = list('')           #= []
empty_list = [*'']              #= []
empty_list = list({})           #= []
empty_list = [*{}]              #= []
empty_list = list(range(0))     #= []
empty_list = [*range(0)]        #= []

Small changes in the expression leads to lists with valid elements.

Py3: Not empty lists

#   caution: these are not empty lists
some_list = ['']                #= ['']
some_list = [range(0)]          #= [range(0)]
some_list = [()]                #= [()]
some_list = [{}]                #= [{}]    

Notes: Empty iterables lead to empty lists

Common iterables in Python are list, tuple, set, dictionary, string or ranges. An empty iterable is converted to an empty list by the list() function. Providing no parameter makes Python assume the default as an empty tuple "()". This then evaluates to an empty list. A blank string, or a range object with no elements result in an empty list. Remember not to use a string with spaces, as spaces do not qualify as empty.

Using the short-hand notation does the same job as the functional form. If using iterables as parameters, the splat operator is needed. The expression [''] will lead to a list with an empty string element, and [*''] will evaluate to an empty list with no element. This small difference is critical.

Both the functional format, and short-hand notation execution times are very similar. When repeatedly creating lists, the short-hand format with splat operator will run slightly faster.

Literal values and expressions

List can contain any Python object, like * numbers * iterables: string, tuple, set, dictionary * functions or class objects * expressions that evaluate to values

Py3: Create list from literals

#   assign list: comma separated strings, integers floats
simple_list = ['u',2,'r','using','Python',3.7]
#= ['u',2,'r','using','Python',3.7]


#   nested list
nested_list = [1,3,5,[2,4,6]]
#= [1,3,5,[2,4,6]]


#   expressions in list are evaluated
value_list = ['python'.upper(), range(3), 1+3/4]
#= ['PYTHON', range(0, 3), 1.75]

Notes: Literals and expressions

List can contain any element like numbers, strings, other nested lists, and even functions and classes. All of the various data types can exist together, unlike arrays which are generally homogeneous.

Literal values are useful to initialize lists. Expressions are evaluated and the value is included in the list. A string case can be changed, or a numeric formula can be evaluated as these are expressions. In contrast, a range object is an iterable, and not evaluated for values.

Numeric list from ranges

A range object is useful to create a series of equally spaced numbers. A combination of series, or more complex functions can generate custom sequence of numbers.

Py3: range iterator to list

#   numbers from a range
num_list = list(range(5))
#= [0, 1, 2, 3, 4]


#   splat operator notation runs slightly faster
#   with splat operator
rng_list = [*range(3), *range(2)]
#= [0, 1, 2, 0, 1]


#   without splat operator
rng_list = [range(3), range(2)]
#= [range(0, 3), range(0, 2)]

Notes: Evaluate an iterator

A range object is an iterator which creates a sequence of numbers. Range is evaluated for the sequence of values it produces and added to the list using the function syntax. To expand the range into its values with shorthand, it is necessary to use the splat '*' operator, or call using a list function.

Iterables with replication

Converting a tuple into a list using list function or splat operator. Replicate existing iterable into a longer sequence.

Py3: convert Tuple to list

#   nested tuple
tup = (1,3,(5,7))

#   nested tuple is preserved
tlist = list(tup)
#= [1,3,(5,7)]


#   short-hand notation with splat
tlist = [*tup]
#= [1,3,(5,7)]


#   replicate existing tuple and convert to list
rlist = list((0,) * 3)
#= [0,0,0]


#   by list comprehension
rlist = [0 for i in range(3)]
#= [0,0,0]


#   boolean tuple
#   here first list is created, then replicated
blist = list((True, False, False)) * 2
#= [True,False,False,True,False,False]


#   nested empty lists
nested = ['', []] * 2
#= ['', [], '', []]

Notes: More of the same items

Tuple is very similar to list. It is almost a subset of list object, except that it is not mutable. A tuple can be hashed, and can be easily converted to a list. Replicating an existing list or tuple builds longer repeating sequences. The mul() method can create copies of iterables concatenated end to end.

Elements of dictionary

A dictionary has both keys and values. A list can be created from keys, values or both. The order of dictionary objects are preserved for Python 3.7+

Py3: From a dictionary

dct = {'a':1, 'b':2, 'c':3}

#   by default the keys are used
ky = list(dct)
#= ['a', 'b', 'c']

#   can also use keys explicitly
ky = list(dct.keys())
#= ['a', 'b', 'c']

#   shorthand for keys to list
ky = [*dct]
#= ['a', 'b', 'c']

#   dictionary values to list
vl = list(dct.values())
#= [1, 2, 3]

#   shorthand for values to list
vl = [*dct.values()]

#   both key and values as paired tuples
im = list(dct.items())
print(im)
#= [('a',1), ('b',2), ('c',3)]

#   keys and values in separate tuples
im = list(zip(*dct.items()))
print(im)
#= [('a','b','c'), (1,2,3)]

Notes: Dictionary conversion

Default behavior of querying a dictionary produces keys. The keys can be explicitly be made available using the keys() method. Keys are unique. Values need to be explicitly accessed for each key, and may not be unique as multiple keys can have same values. Key and value pairs can be sent to a list using the items() method. Transposing these using zip function can yield a tuple of keys and values nested within a list.

Letters from string

The list function converts a string into component letter sequences.

Py3: String to letters

text = 'string to list'
letters = list(text)
#= ['s','t','r','i','n','g',' ','t','o',' ','l','i','s','t']

#   using shorthand splat, a little faster too
letters = [*text]
#= ['s','t','r','i','n','g',' ','t','o',' ','l','i','s','t']

# using extend method
letters = []
letters.extend(text)
#= ['s','t','r','i','n','g',' ','t','o',' ','l','i','s','t']

#   using concatenate iterable operation
letters = []
letters += text
#= ['s','t','r','i','n','g',' ','t','o',' ','l','i','s','t']    

Notes: Letters from string

To convert into text string into a list of letters, we can call the list function, or use the shorthand to expand the iterable. A text string is an iterable. So each letter is accessed and added to a list, creating a list of individual letters in order of first appearance within parent string. This is a very useful method in processing text.

The iadd() and extend methods concatenates iterables and so achieves the same result of converting a string to letters.

Words and sub-strings

Segmenting an original string into smaller parts by splitting at positions with matching characters.

Py3: String segmented into sub-strings

text = 'string to; list'

#   split text; default is whitespace characters
words = list(text.split())
#= ['string','to;','list']

#   explicitly mentioning space as discriminator
words = list(text.split(' '))
#= ['string','to;','list']

#   different letter yields substrings
substr = list(text.split('t'))
#= ['s', 'ring ', 'o; lis', '']    

#   multiple whitespaces
multispaces = 'test    this '

#   default: whitespaces
words = multispaces.split()
#= ['test', 'this']

#   explicitly single space
words = multispaces.split(' ')
#= ['test','','this','']

#   explicitly double space
words = multispaces.split('  ')
#= ['test','this '] 

Using the regular expressions 'regex' module to perform complex splits with multiple separators and discriminators.

Py3: Splitting with regex

import re

mtext = '''A long, multi-line text to
split into words requires the use
of regular expressions module.
Built-in list split method works
for simple text strings.'''

#   split on everything except a-z0-9 and _
#   remove any zero length empty words
words = [word for word in re.split(r'[^\w]+', mtext.lower()) if word]

#= ['a', 'long', 'multi', 'line', 'text', 'to', 'split', 
#   'into', 'words', 'requires', 'the', 'use', 'of', 'regular',
#   'expressions', 'module', 'built', 'in', 'list', 'split', 
#   'method', 'works', 'for', 'simple', 'text', 'strings']       

Notes: Sub-string list

By default a string is split at space or whitespace locations. The space itself is not returned as an element. The elements in the list produced are words, but may also contain punctuations, if present.

Only a single splitting separator can be used at a time, but the pattern to split on can be multi-character. If using default split, without specifying character to split with - multiple whitespaces are treated as one. However explicitly specifying the whitespace can return empty strings as list elements. To split on multiple separators, the regular expression module can be used.

Custom functions and element assignment

Built-in list() function returns list objects. Custom Python functions can also return list objects.

Py3: Return list object

#   function to calculate sum of numbers from 0 to n
#   returns [sum(0), sum(1), sum(2), sum(3)...sum(n)]
def cumulative(n):
    lst = []
    sum = 0
    for num in range(n):
        sum += num
        lst += [sum]
    # return a list of numbers
    return lst

#   cumulative series of numbers
c = cumulative(5)
print(c)
#= [0, 1, 3, 6, 10]

c = cumulative(7)
print(c)
#= [0, 1, 3, 6, 10, 15, 21]

Not all the elements need to be assigned into a single variable. It is possible to assign the whole list to a single variable, or parts of it can be distributed between multiple variables.

Py3: Assignment to variables

#   assign one element to first and last variable
#   rest assigned as a list to the middle
v1, *v2, v3 = [0,1,2,3,4]
#= v1=0, v2=[1,2,3], v3=4
# note that v1, v3 are not list objects

#   other possible assignments with 3 variables
*v1, v2, v3 = [0,1,2,3,4]
#= v1=[0,1,2], v2=3, v3=4

v1, v2, *v3 = [0,1,2,3,4]
#= v1=0, v2=1, v3=[2,3,4]

#   when elements are less than variables
v1, v2, *rest = [0, 1]
#= v1=0, v2=0, rest=[]

Py3: Function for custom distribution of elements

#   distribute elements between 3 variables
#   pre and post element count are provided
def distribute(lst, pre, pst):
    return(lst[:pre], lst[pre:-pst], lst[-pst:])

#   assign 2 from start to v1
#   assign 3 from end to v3
#   rest to v2
#   all are returned as list objects
v1, v2, v3 = distribute([1,2,3,4,5], pre=2, pst=3)
#= v1=[1,2], v2=[], v3=[3,4,5]

Notes: Assigning list objects

Elements of a list can be distributed to multiple variables, with leftovers accumulating in a leftover list variable. This is very useful to create fifo (first in first out), or lifo (last in first out) data structures.

Nested multi-valued generator

A generator function does not return a complete list. It produces a single result each time it is called. The single result can be a multi-valued object like a tuple.

Py3: Generator yields a tuple

#   generator yielding tuple with 3 elements
#   iterated from 0 to n
def numgen(n):
    for i in range(n):
        yield (i, i**2, i**3)

#   create a nested list of 4 tuples
#   each with 3 elements
tuplist = list(numgen(4))
#= [(0,0,0), (1,1,1), (2,4,8), (3,9,27)]

#   create a flat list from the nested tuples
#   using the built-in sum function
numlist = list(sum(tuplist, ()))
#= [0,0,0,1,1,1,2,4,8,3,9,27]

Tuples with text and numbers.

Py3: Mixed type tuples in lists

#   mixed type tuples in list
elements = ['H','He','Li', 'Be']
atomic_w = (1, 4, 7, 9)

#   combine into a list of tuples
combo = list(zip(elements, atomic_w))
#= [('H',1),('He',4), ('Li',7), ('Be',9)]    

Notes: Nested lists

Instead of dealing with flat lists, it may make sense in many cases to pack and organize results in a nested structure contained within a container list. A range iterator produces just numbers, but all types of objects can be packed into subgroups and stored sequentially within a list. This can include other lists, tuples, functions or whole classes.

Comprehension of generators

Fibonacci generator can create an infinite sequence. Using a dummy co-iterator to limit the number of elements returned.

Py3: Generator yields Fibonacci numbers

#   Fibonacci is coded as an open ended generator
#   capable of producing an infinite sequence
#   fib = list(fibonacci()) will cause an endless loop
def Fibonacci():
    x, y = (0, 1)
    while True:
        yield x
        x, y = y, x + y

#   use range as co-iterator to stop after 10 iterations
#   only limited values are returned
#   iteration stops as any one co-iterator runs out of items
fib = [f for r, f in zip(range(10), Fibonacci())]
#= [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Notes: Controlling an infinite loop

To ensure there is no runaway looping within the Fibonacci generator, a control needs to be inserted to stop iteration. Using the built-in zip function achieves this purpose by supplying a finite range. The zip function simultaneously iterates over the range and Fibonacci generator. The iteration stops when the range function reaches 9. The generator returns a finite list of numbers.

Evaluating list of functions

Applying a list of functions on a list of numbers, creating a two dimensional list of results.

Py3: List of functions

#   func #1: power and modulo
def pow_mod(pow=0, mod=1):
    def calc(x):
        return (x**pow) % mod
    return calc

#   __callable__ class for cumulative sum
class accumulate:
    def __init__(self, epoch=0):
        self.sum = epoch

    def __call__(self, x):
        self.sum += x
        return self.sum

#   generate a list of functions
#   -> lambda function returning unchanged number
#   -> (n^2) % 5 
#   -> cumulative of n
funcs = [lambda x: x, pow_mod(2,5), accumulate()]

#   define x: a list of values
x = [1,2,3,4,5,6,7,8,9,10]

#   evaluate each function with x
feval = [list(map(f, x)) for f in funcs]

# list of lists are returned for each function
#= [ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
#    [1, 4, 4, 1, 0, 1, 4, 4, 1, 0],
#    [1, 3, 6, 10, 15, 21, 28, 36, 45, 55] 
#  ]

#   reorganized, transposed for each number
feval_t = [*zip(*feval)]
#= [ (1,1,1), (2,4,3), (3,4,6), 
#    (4,1,10), (5,0,15), (6,1,21), 
#    (7,4,28), (8,4,36), (9,1,45), 
#   (10,0,55)
#  ]

Notes: List of functions

Python lists are versatile containers, and can house callable functions. Three types are shown here namely lambda functions, regular functions, and class based objects. Using the map() function, each of the three process a sequence of numbers. The results are stored as a two dimensional list of lists. The arrangement can be altered to group results by function, or by original value.

Converting Numpy arrays

Numpy is a very handy module for scientific calculation of arrays. It is easy to convert linear lists into numpy ndarrays and back.

Py3: 1D numpy arrays

#   simple numpy array
#   both methods yield same result
import numpy as np

#   list of numbers
lstinp = [1, 1/2, 1/3]
#= [1, 0.5, 0.3333333333333333]

#   list to numpy array  
na = np.array(lstinp)

#---METHOD# 1: built-in list conversion method
lstout = list(na)
#= [1.0, 0.5, 0.3333333333333333]

#---METHOD# 2: numpy method to return list
lstout = na.tolist()
#= [1.0, 0.5, 0.3333333333333333]

Numpy allows linear arrays to be reshaped into multi-dimensional arrays. The results can be returned as lists after conversion.

Py3: 2D numpy arrays

import numpy as np

#   create 2d numpy array
lstinp = [1, 1/2, 1/4, 1/8]
na = np.array(lstinp).reshape((2,2))
#= [[1.    0.5  ]
#  [0.25  0.125]]

#---METHOD# 1: built in conversion looks complicated
lstout = list(na)
#= [array([1. , 0.5]), array([0.25 , 0.125])]

#---METHHOD# 2: numpy method for conversion
#   this is the better solution
lstout = na.tolist()
#= [[1.0, 0.5], [0.25, 0.125]]

Notes: Numpy conversions

Python's strength lies in multiple scientific modules that are available. Many numerical modules are based on Numpy. Converting numpy ndarrays back and forth is a common activity.

Although lists can contain any object type, the ndarrays are totally numerical and homogeneous. This means they can only be integers, booleans, floats, complex with fixed bit-size. This makes ndarrays faster than lists. Numpy provides dedicated methods to convert arrays back to lists. It is preferable to use numpy methods to convert back to list. Built-in list methods are not always able to convert ndarrays into regular format, especially for multi-dimensional arrays. Values can also be truncated and rounded off based on the accuracy of the floating point numbers in use for the operating system.

Popular content

python programming

Read and write lists with Pickle, Array and Pandas

python programming

Flatten nested list or generate blocks of nested lists

python programming

For loop and control statements

python programming

Clear list using inplace and standard methods

python programming

List comprehension with nested conditions

python programming

Concatenate list elements using add, append, extend

python programming

Enumerate and custom counters like skip and loop

python programming

Count number of elements, and memory allocated

python programming

Remove duplicate list elements

python programming

Statistics with numeric lists of integers, fractions, and decimals.

New content

python programming

Read and write lists with Pickle, Array and Pandas

python programming

Is element in list?

python programming

Dictionary merge common key groups

python programming

Packaging loops with zip

python programming

Concatenate list elements using add, append, extend

python programming

List comprehension with nested conditions

python programming

Flatten nested list or generate blocks of nested lists

python programming

Enumerate and custom counters like skip and loop

python programming

Range integer sequences

python programming

For loop and control statements