Getting values with indexes
title: Python Lists and Tuples description: In python, Lists are are one of the 4 data types in Python used to store collections of data.
Lists are one of the 4 data types in Python used to store collections of data.
['John', 'Peter', 'Debora', 'Charles']
Getting values with indexes
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
>>> furniture[1]
>>> furniture[2]
>>> furniture[3]
Negative indexes
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
>>> furniture[-3]
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
Getting sublists with Slices
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
>>> furniture[1:3]
>>> furniture[0:-1]
>>> furniture[:2]
>>> furniture[1:]
>>> furniture[:]
Slicing the complete list will perform a copy:
>>> spam2 = spam[:]
>>> spam.append('dog')
>>> spam
>>> spam2
Getting a list length with len()
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> len(furniture)
Changing values with indexes
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0] = 'desk'
>>> furniture
>>> furniture[2] = furniture[1]
>>> furniture
>>> furniture[-1] = 'bed'
>>> furniture
Concatenation and Replication
>>> [1, 2, 3] + ['A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
>>> my_list = [1, 2, 3]
>>> my_list = my_list + ['A', 'B', 'C']
>>> my_list
Using for loops with Lists
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> for item in furniture:
... print(item)
Getting the index in a loop with enumerate()
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> for index, item in enumerate(furniture):
... print(f'index: {index} - item: {item}')
Loop in Multiple Lists with zip()
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> price = [100, 50, 80, 40]
>>> for item, amount in zip(furniture, price):
... print(f'The {item} costs ${amount}')
The in and not in operators
>>> 'rack' in ['table', 'chair', 'rack', 'shelf']
>>> 'bed' in ['table', 'chair', 'rack', 'shelf']
>>> 'bed' not in furniture
>>> 'rack' not in furniture
The Multiple Assignment Trick
The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> table = furniture[0]
>>> chair = furniture[1]
>>> rack = furniture[2]
>>> shelf = furniture[3]
You could type this line of code:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> table, chair, rack, shelf = furniture
>>> table
>>> chair
>>> rack
>>> shelf
The multiple assignment trick can also be used to swap the values in two variables:
>>> a, b = 'table', 'chair'
>>> a, b = b, a
>>> print(a)
>>> print(b)
The index Method
The index
method allows you to find the index of a value by passing its name:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture.index('chair')
Adding Values
append()
append
adds an element to the end of a list
:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture.append('bed')
>>> furniture
insert()
insert
adds an element to a list
at a given position:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture.insert(1, 'bed')
>>> furniture
Removing Values
del()
del
removes an item using the index:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> del furniture[2]
>>> furniture
>>> del furniture[2]
>>> furniture
remove()
remove
removes an item with using actual value of it:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture.remove('chair')
>>> furniture
pop()
By default, pop
will remove and return the last item of the list. You can also pass the index of the element as an optional parameter:
>>> animals = ['cat', 'bat', 'rat', 'elephant']
>>> animals.pop()
'elephant'
>>> animals
['cat', 'bat', 'rat']
>>> animals.pop(0)
'cat'
>>> animals
['bat', 'rat']
Sorting values with sort()
>>> numbers = [2, 5, 3.14, 1, -7]
>>> numbers.sort()
>>> numbers
furniture = ['table', 'chair', 'rack', 'shelf']
furniture.sort()
furniture
You can also pass True
for the reverse
keyword argument to have sort()
sort the values in reverse order:
>>> furniture.sort(reverse=True)
>>> furniture
If you need to sort the values in regular alphabetical order, pass str.lower
for the key keyword argument in the sort() method call:
>>> letters = ['a', 'z', 'A', 'Z']
>>> letters.sort(key=str.lower)
>>> letters
You can use the built-in function sorted
to return a new list:
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> sorted(furniture)
The Tuple data type
tuples
are immutable objects, lists
are mutable. This means that tuples cannot be changed while the lists can be modified. Tuples are more memory efficient than the lists.
>>> furniture = ('table', 'chair', 'rack', 'shelf')
>>> furniture[0]
>>> furniture[1:3]
>>> len(furniture)
The main way that tuples are different from lists is that tuples, like strings, are immutable.
Converting between list() and tuple()
>>> tuple(['cat', 'dog', 5])
>>> list(('cat', 'dog', 5))
>>> list('hello')