Tutorial - Python 3 basics
Lists, Tuples and Dictionaries

Posted September 28, 2018 by Rokas Balsys

Sometimes people are confused about tuples and lists, due to their similarities, but in fact these two structures are substantially different, so in this tutorial we will show that.

###### Python Lists:

A list contains items separated by commas and enclosed within square brackets ([]). Lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data types.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to the end -1. As we already mentioned in previous tutorial, the plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.
Example:

list = ['qwerty', 7777, 1.11, 'Tomm', 66.6]
tinylist = [987, 'Tomm']

print (list)            # Prints complete list
print (list[0])         # Prints first element of the list
print (list[1:3])       # Prints elements starting from 2nd till 4th
print (list[2:])        # Prints list starting from 3rd character
print (tinylist * 2)    # Prints list two times
print (list + tinylist) # Prints concatenated list


In a print Shell this will produce the following result:

['qwerty', 7777, 1.11, 'Tomm', 66.6]
qwerty
[7777, 1.11]
[1.11, 'Tomm', 66.6]
[987, 'Tomm', 987, 'Tomm']
['qwerty', 7777, 1.11, 'Tomm', 66.6, 987, 'Tomm']


###### Python Tuples

Python tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ([ ]) and their elements and size can be changed, while tuples are enclosed in parentheses (( )) and cannot be updated or changed. Tuples can be thought of as read-only lists.
Example:

tuple = ('qwerty', 7777, 1.11, 'Tomm', 66.6)
tinytuple = (987, 'Tomm')

print (tuple)            # Prints complete tuple
print (tuple[0])         # Prints first element of the list
print (tuple[1:3])       # Prints elements starting from 2nd till 4th
print (tuple[2:])        # Prints tuple starting from 3rd character
print (tinytuple * 2)    # Prints tuple two times
print (tuple + tinytuple)# Prints concatenated tuple


In a print Shell this will produce the following result:

('qwerty', 7777, 1.11, 'Tomm', 66.6)
qwerty
(7777, 1.11)
(1.11, 'Tomm', 66.6)
(987, 'Tomm', 987, 'Tomm')
('qwerty', 7777, 1.11, 'Tomm', 66.6, 987, 'Tomm')


If we would try to update a tuple:

>>> tuple[2] = 200


We will receive following error, saying that it does not support item assignment:

TypeError: 'tuple' object does not support item assignment


###### Python Dictionary:

Python dictionaries are a data structures that are very similar to associative arrays. They are non-ordered and contain "keys" and "values." A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries are defined by curly braces ({ }) and values can be assigned and accessed using square braces ([ ]).

dict = {}
dict['first']   = "This is first"
dict[2]         = "This is second"

tinydict = {'name': 'Tomm','code':6434, 'hobby': 'reading'}

print (dict['first'])       # Prints value for 'one' key
print (dict[2])             # Prints value for 2 key
print (tinydict)            # Prints complete dictionary
print (tinydict.keys())     # Prints all the keys
print (tinydict.values())   # Prints all the values


In a print Shell this will produce the following result:

This is first
This is second
{'name': 'Tomm', 'code': 6434, 'hobby': 'reading'}
dict_keys(['name', 'code', 'hobby'])


If we would like to insert something new:

tinydict['City'] = 'London'


By writing print(tinydict) in a print Shell this will produce the following result:

{'name': 'Tomm', 'code': 6434, 'hobby': 'reading', 'City': 'London'}


Next we can do is to remove 'code' component from our dictionary:

del tinydict['code']


Then we will have following dictionary:

{'name': 'Tomm', 'hobby': 'reading', 'City': 'London'}


We can make group of people dictionary with their age and living city, example:

Dict = {'Tomm':[25,'Alabama'], 'Bob':[28, 'California'], 'Alice':[21,'Florida'], 'Kevin':[35,'Delaware']}
print(Dict['Kevin'])
print(Dict['Bob'][1])


In a print Shell this will produce Kevin's age and living city, about Bob it will print only his living city:

[35, 'Delaware']
California


Now we covered Lists, Tuples and Dictionaries. Now you know difference between them, so you can move to next tutorial: