Lists
collection of elements with defined order
flexible: elements can be added, removed, replaced: d.append('B')
indexed elements, zero-based: d[0]
mylist = ['Antje', 'Mike', 'Michel']
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
Dictionaries
similar to sets (unique, no order), but with the possibility to add data to each entry
no particular order (no index)
elements are unique (key identifier)
flexible: elements and data can be added, removed, replaced
d['Antje']=('Barcelona',1987)
→ Standard single key dictionaries
d['Antje']['year']=1987
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 (without split into characters)
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