Data structures

    • collection of elements with defined order

    • flexible: elements can be added, removed, replaced: d.append('B')

    • indexed elements, zero-based: d[0]

read more ...

Tuples

    • like lists that cannot be changed

    • ordered (indexed)

    • constant: elements cannot be changed

Sets

    • collection of unique elements (duplicated values are removed)

    • no particular order (no index)

    • flexible: elements can be added, removed, replaced: d.add('B')

    • logical operations between sets: d1.union(d2)

    • loop / test: if 'B' in d:

    • a set is like a dictionary with no values

    • like sets, but with the possibility to add data to each element: d['Mike']=('Berlin',1983)

    • no particular order (no index)

    • elements are unique (key-words)

    • flexible: elements and data can be added, removed, replaced

read more ...

Note...

d={} is an empty dictionary

d={'A'} is a set

d=set() is an empty set

d=() is an empty tuple

d=[] is en empty list

To check the data type use:

type(d)

<type 'list'>

Convert data types

# check type

mylist = ['A', 'B', 'A']

type(mylist)

<type 'list'>

# convert list to set

myset=set(mylist)

set(['A', 'B'])

type(myset)

<type 'set'>

# convert text-string into single element list

s = 'filename.txt'

myFileList = [s]

['filename.txt']


"if" check for data type

"if, else" data type testing

x = ['A', 'B']

if type(x) is list:

print('YES, x is a list')

else:

print('NO, x is not a list')

YES, x is a list