Python – Sets and Tuples

 

What are they?

A Set is an unordered, mutable collection that will not retain duplicate values.

A Tuple is an ordered, immutable, collection that can accept duplicates.

How are they created?

Set: 

three basic ways –

Using the set( ) function: s = set( )

Converting a list to a set: s = set(list_name) or s = set( [obj 1, obj 2, obj 3, …] )

Braces notation: s = {‘a’, ‘b’, 1, 2, 2}

Note: You cannot have nested lists when converting a list to a set

Tuple:

Using parenthesis: t = ( )

by assignment using parenthesis: t = (1,2,3,4,5), or without: t = 1,2,3,4,5

Note: to create a tuple with a single entry, you must include a comma. Here is an example: t2 = ( 1, ) This is a rule of the language, a syntactic quirk of Python. If you do this: t2 = ( 1 ), and use the type function: type(t2), it will return an ‘int’ object type, not the expected tuple.

When to use them?

Set[s]: Whenever you need to create a collection of distinct values, and/or apply mathematical set theory to those values ( intersection, union, difference, etc. ). Or, if you need to convert an existing list containing duplicate values, you can specify it in the set function.

Tuple[s]: Used mostly for values that need to remain constant.

Methods to use:

You can use the dir( ) function to find all methods and “dunders” contained for the type.

dir(set) | dir(tuple)

Since Tuples are immutable, they do not contain many of the common methods to append, clear, add, etc. In fact, only two exist: Index and Count

Example Code:

Here are some code examples from my Jupyter Notebook.  I’ve included some basic examples for both Sets and Tuples.  *Click on the image to view a larger picture.

 

Summary:

In this blog entry I’ve included the definition of a Set and a Tuple.  I have reviewed their usage, creation,  and shown some example code using both.

My next blog will focus on using Python with PyODBC to connect to an instance of SQL Server 2016, perform a SELECT statement on an existing table.  I’ll then use a For Each loop to review the data returned.  It’s very simple to do, and only requires a minimum of coding to perform.

Knowledge is Power!


References:

Python Tuples:  https://docs.python.org/3/library/stdtypes.html#tuple

Python Sets:  https://docs.python.org/3/library/stdtypes.html#set

Jupyter Notebook:  http://jupyter.org/

Python – Lists

Lists are similar to C array types.  A list can accept any Python object, including other lists.

You can create a list in several ways:

a = list()    # creates an empty list (a)

a = [ ]        # using brackets to create an empty list (a)

a = [1,2,3,4,5]    # creates a list of integers 1-5

a = [1, ‘SQL’, 3.45, [1,2,3] ]    # Creates a list of various data types: integer, string, float, and another list

a = [ (1,2), (101,102) ]    # Creates a list of Tuples (later)

a = [ {‘City’: ‘Seattle’, ‘State’: ‘Washington’, ‘Population’: ‘2.3M’} ]   # Creates a list of Dictionaries (later)

Range:

You can create a list of integers using the range() function:

x = list(range(9))

x = list(range(1,10))

x = list(range(1,10,2))   # 1-10, by 2 (called the ‘step’)

prints:

[1, 2, 3, 4, 5, 6, 7, 8][1, 2, 3, 4, 5, 6, 7, 8, 9][1, 3, 5, 7, 9]   # note: all odd values

 

 

You can convert another object to a list:

x = ‘this is a string’

y = list(x)

Python lists keep insertion order, that is, when you access the list contents, they will be in the order they

were inserted.  There are other collections that do not follow this functionality.

You can print the list:

Python 3.x:

print(a)

Python 2.x:

print a

List are mutable, that is, you can change them after creation.

Append to the end of a list:

a.append(‘Server’)

Insert to a specific location within the list:

list_name.insert(index, value)

a.insert(2, ‘Server’)

Clear the list of all it’s contents:

a.clear()

You can also just overwrite the list content:

a = 1,2,3,4,5

a = ‘Python is Cool!’

One often desirable function to perform on lists is to sort them:

a = [1,2,33,5,7,22,101,3]

a.sort()

print(a)

prints:

[1, 2, 3, 5, 7, 22, 33, 101]

This is an in-place sort, meaning it does not return a sorted list, it sorts the existing list values.

The sorted() function returns a new sorted list:

old = [1, -1, 22, 6]

print(old)

new = sorted(old)

print(new)

prints:

[1, -1, 22, 6][-1, 1, 6, 22]

 


 

References:

Python Website:  www.python.org

Python lists:  list_URL