Data structures

Lists

  • 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

Dictionaries

  • 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

It's a bit confusing..  to check the data type use:
>>> type(d)
<type 'list'>

Convert data type

# check type
>>> mylist = ['A', 'B', 'A']
>>> type(mylist)
<type 'list'>

# convert list to set
>>> myset=set(mylist)
>>> myset
set(['A', 'B'])
>>> type(myset)
<type 'set'>

Subpages (2): Dictionary Lists