Data structures

Python by Examples

Lists

        mylist = ['Antje', 'Mike', 'Michel']

       List data structures

Tuples

Sets

Dictionaries

       d['Antje']=('Barcelona',1987)

        → Standard single key dictionaries

       d['Antje']['year']=1987

        → Multi-key dictionaries


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