Tuples, Lists, and Dictionaries
By Kelvin Kiprono in Python
September 1, 2026
TUPLES
What Is a Tuple?
The word tuple comes from mathematics, where it’s used to describe a finite ordered sequence of values.For example , (1,2,3) is a tuple containing three integers.
Tuples are ordered because their elements appear in an ordered fashion.
How to Create a Tuple
There are a few ways to create a tuple in Python. We’ll cover two of them:
- Tuple literals
- The built-in tuple()
Tuple Literals
Just like a string literal is a string that is explicitly created by surrounding some text with quotes, a tuple literal is a tuple that is written out explicitly as a comma-separated list of values surrounded by parentheses. Here’s an example of a tuple literal:
my_first_tuple = (1, 2, 3)
Unlike strings, which are sequences of characters, tuples may contain any type of value, including values of different types. The tuple (1, 2.0, “three”) is perfectly valid.
my_next_tuple=(1,2,'three','Kelvin')
To create a tuple containing the single value 1, you need to include a comma after the 1:
x=(1,)
type(x)
## <class 'tuple'>
The Built-In tuple()
You can also use the built-in tuple() to create a tuple from another sequence type, such as a string:
tuple("Python")
## ('P', 'y', 't', 'h', 'o', 'n')
Similarities Between Tuples and Strings
Tuples and strings have a lot in common. Both are sequence types with finite lengths, both support indexing and slicing, both are immutable, and both can be iterated over in a loop.
The main difference between strings and tuples is that the elements of tuples can be any kind of value you like, whereas strings can only contain characters.
name =("David")
name[1]
## 'a'
The index notation [1] after the variable name tells Python to get the character at index 1 in the string “David”. Since counting starts at zero,the character at index 1 is the letter “a”.
Tuples also support index notation:
values =[1,3,4,7,9,0]
values[3]
## 7
Another feature that strings and tuples have in common is slicing.
values =[1,3,4,7,9,0]
values[2:4]
## [4, 7]
The slice values[2:4] creates a new tuple containing all the integers in values starting from position 2 and going up to but not including position 4.
Tuples are immutable
This means you cannot change the value of an element in a tuple once it has been created.If you do try to change the values then you’ll raise a TypeError.
Tuples are iterable
You can loop over them
name = ("LEONARD")
for i in name:
print(i)
## L
## E
## O
## N
## A
## R
## D
Checking for Existence of Values With (in)
You can check whether a value is contained in a tuple with the in keyword.
name
## 'LEONARD'
"O" in name
## True
LISTS
On the surface, lists look and behave a lot like tuples. With lists, you can use index and slice notation, check for the existence of an element using in, and iterate over elements using a for loop.
Unlike tuples, however, lists are mutable, meaning you can change the value at a given index even after the list has been created.
Creating Lists A list literal looks almost exactly like a tuple literal, except that it’s surrounded by square brackets ([]) instead of parentheses:
names=["Kelvin","Nathan","Judy","Cate","Natalie"]
names
## ['Kelvin', 'Nathan', 'Judy', 'Cate', 'Natalie']
type(names)
## <class 'list'>
Like tuple values, list values are not required to be of the same type
Basic List Operations
Indexing and slicing operations work on lists the same way they do on tuples. You can access list elements using index notation:
names=["Kelvin","Nathan","Judy","Cate","Natalie"]
names[1]
## 'Nathan'
names[2:4]
## ['Judy', 'Cate']
You can check for the existence of list elements using the in operator:
"Natalie" in names
## True
Lists are iterable, which means you can iterate over them with a for loop:
numbers=[1,2,3,5,7]
for i in numbers:
print(i)
## 1
## 2
## 3
## 5
## 7
Changing Elements in Lists
You can think of a list as a sequence of numbered slots.Each slot holds a value, and every slot must be filled at all times. But you can swap out the value in a given slot with a new one whenever you want.
The ability to swap values in a list for other values is called mutability. Lists are mutable. The elements of tuples can’t be swapped for new values, so tuples are said to be immutable.
To swap one value in a list with another, assign the new value to a slot using index notation:
names=["Kelvin","Nathan","Judy","Cate","Natalie"]
names
## ['Kelvin', 'Nathan', 'Judy', 'Cate', 'Natalie']
names[0]
## 'Kelvin'
names[0]="Sharon"
The value at index 0 changes from “Kelvin” to “Sharon”
names
## ['Sharon', 'Nathan', 'Judy', 'Cate', 'Natalie']
You can change several values in a list at once with a slice assignment:
names[1:3]=["Sofia","Scott"]
names
## ['Sharon', 'Sofia', 'Scott', 'Cate', 'Natalie']
Adding and Removing Elements
list.insert()
The list.insert() method is used to insert a single new value into a list. It takes two parameters, an index i and a value x, and inserts the value x at index i in the list.
colors = ["red", "yellow", "green", "blue"]
#insert color "orange" at index 1
colors.insert(1,"orange")
colors
## ['red', 'orange', 'yellow', 'green', 'blue']
list.pop()
The list.pop() method takes one parameter, an index i, and removes the value from the list at that index. The value that is removed is returned by the method:
names
## ['Sharon', 'Sofia', 'Scott', 'Cate', 'Natalie']
names.pop(3)
## 'Cate'
The value at index 3 is removed and assigned to the variable names. When you inspect the names list, you can see that the string “cate” has indeed been removed
names
## ['Sharon', 'Sofia', 'Scott', 'Natalie']
list.append()
This method is used to append a new element to the end of a list:
names.append("Susan")
names
## ['Sharon', 'Sofia', 'Scott', 'Natalie', 'Susan']
list.extend()
This method is used to add several new elements to the end of a list:
names.extend(["Milicent","Francis"])
names
## ['Sharon', 'Sofia', 'Scott', 'Natalie', 'Susan', 'Milicent', 'Francis']
Nesting, Copying, and Sorting Tuples and Lists
Nesting Lists and Tuples
Lists and tuples can contain values of any type. That means lists and tuples can contain lists and tuples as values. A nested list or tuple is a list or tuple that is contained as a value in another list or tuple.
For example, the following list has two values, both of which are other lists:
two_by_two = [[1,2],[3,4]]
two_by_two
## [[1, 2], [3, 4]]
len(two_by_two)
## 2
two_by_two[0]
## [1, 2]
two_by_two[0][1]
## 2
First, Python evaluates two_by_two[0] and returns [1, 2]. Then Python evaluates [1, 2][1] and returns the second element, 2.
Sorting Lists Lists have a .sort() method that sorts all of the items in ascending order. By default, the list is sorted in alphabetical or numerical order depending on the type of elements in the list:
names
## ['Sharon', 'Sofia', 'Scott', 'Natalie', 'Susan', 'Milicent', 'Francis']
names.sort()
names
## ['Francis', 'Milicent', 'Natalie', 'Scott', 'Sharon', 'Sofia', 'Susan']
nums=[23,21,54,2,34,87,51]
nums.sort()
nums
## [2, 21, 23, 34, 51, 54, 87]
DICTIONARIES
Python dictionaries, like lists and tuples, store a collection of objects. However, instead of storing objects in a sequence, dictionaries hold information in pairs of data called key-value pairs. That is, each object in a dictionary has two parts: a key and a value.
The key in a key-value pair is a unique name that identifies the value part of the pair.
cities = {
"Kenya" : "Nairobi",
"Uganda" : "Kampala",
"Congo" : "Kinshasha"}
cities
## {'Kenya': 'Nairobi', 'Uganda': 'Kampala', 'Congo': 'Kinshasha'}
Each key is separated from its value by a colon,each key value pair is separated by a comma and the entire dictionary is closed in curly brackets. When accessing dictionary values,you enclose the corresponding key in square brackets.
cities["Kenya"]
## 'Nairobi'
Adding and Removing Values in a Dictionary
Like lists, dictionaries are mutable data structures. This means you can add and remove items from a dictionary.
cities["Tanzania"]="Dodoma"
cities
## {'Kenya': 'Nairobi', 'Uganda': 'Kampala', 'Congo': 'Kinshasha', 'Tanzania': 'Dodoma'}
To remove an item from a dictionary, use the del keyword with the key for the value you want to delete:
del cities["Kenya"]
cities
## {'Uganda': 'Kampala', 'Congo': 'Kinshasha', 'Tanzania': 'Dodoma'}
If you try to access a value in a dictionary using a key that doesn’t exist,then Python raises a KeyError:
cities["Kenya"]
## KeyError: 'Kenya'
You can check if a key exists in a dictionary using in
"Kenya" in cities
## False