Lists
Lists¶
- Lists are used to store multiple items in a single variable.
- Lists can hold items of different data types such as string, integer, float
In [2]:
Copied!
## creating a list
fruits_list = ['apple', 'banana', 'cherry', 10, 20, 30, 111.4]
print(fruits_list)
## creating a list
fruits_list = ['apple', 'banana', 'cherry', 10, 20, 30, 111.4]
print(fruits_list)
['apple', 'banana', 'cherry', 10, 20, 30, 111.4]
List is Mutable¶
In [3]:
Copied!
fruits_list = ['apple', 'banana', 'cherry']
fruits_list[0] = 'mango'
print(fruits_list)
fruits_list = ['apple', 'banana', 'cherry']
fruits_list[0] = 'mango'
print(fruits_list)
['mango', 'banana', 'cherry']
Indexing and Slicing¶
In [26]:
Copied!
## creating the list
numbers_list = [53, 93, 35, 1, 65, 26, 47, 3]
## get the length of the list
print(len(numbers_list))
## creating the list
numbers_list = [53, 93, 35, 1, 65, 26, 47, 3]
## get the length of the list
print(len(numbers_list))
8
In [13]:
Copied!
## indexing in list
print(numbers_list[0])
print(numbers_list[5])
print(numbers_list[-2])
## indexing in list
print(numbers_list[0])
print(numbers_list[5])
print(numbers_list[-2])
53 26 47
In [4]:
Copied!
## slicing in list
numbers_list = [53, 93, 35, 1, 65, 26, 47, 3]
## slicing with positive numbers
print('-----------------------------')
print('slicing with postive numbers')
print('-----------------------------')
print(numbers_list[1:4])
print(numbers_list[:3])
print(numbers_list[3:])
## slicing with negative numbers
print('-----------------------------')
print('slicing with negative numbers')
print('-----------------------------')
print(numbers_list[-5:-3])
## slicing in list
numbers_list = [53, 93, 35, 1, 65, 26, 47, 3]
## slicing with positive numbers
print('-----------------------------')
print('slicing with postive numbers')
print('-----------------------------')
print(numbers_list[1:4])
print(numbers_list[:3])
print(numbers_list[3:])
## slicing with negative numbers
print('-----------------------------')
print('slicing with negative numbers')
print('-----------------------------')
print(numbers_list[-5:-3])
----------------------------- slicing with postive numbers ----------------------------- [93, 35, 1] [53, 93, 35] [1, 65, 26, 47, 3] ----------------------------- slicing with negative numbers ----------------------------- [1, 65]
In [5]:
Copied!
## with steps
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers_list[::2])
## reverse the list
print(numbers_list[::-1])
## with steps
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers_list[::2])
## reverse the list
print(numbers_list[::-1])
[1, 3, 5, 7, 9] [9, 8, 7, 6, 5, 4, 3, 2, 1]
In [6]:
Copied!
temp_list = ['apple', 1, 2]
temp_list = ['apple', 1, 2]
In [10]:
Copied!
a = 2
print(type(temp_list))
a = 2
print(type(temp_list))
<class 'list'>
List Methods¶
Method | Description | Example Code | Output |
---|---|---|---|
append() |
Adds an item to the end of the list | lst = [1, 2] lst.append(3) |
[1, 2, 3] |
extend() |
Adds all elements of another list | lst = [1, 2] lst.extend([3, 4]) |
[1, 2, 3, 4] |
insert() |
Inserts item at given index | lst = [1, 3] lst.insert(1, 2) |
[1, 2, 3] |
remove() |
Removes first occurrence of value | lst = [1, 2, 3] lst.remove(2) |
[1, 3] |
pop() |
Removes and returns item at index (default last) | lst = [1, 2, 3] lst.pop() |
3 , list: [1, 2] |
clear() |
Removes all items from the list | lst = [1, 2, 3] lst.clear() |
[] |
index() |
Returns first index of value | lst = [10, 20, 30] lst.index(20) |
1 |
count() |
Counts how many times value appears | lst = [1, 1, 2, 1] lst.count(1) |
3 |
sort() |
Sorts the list in ascending order | lst = [3, 1, 2] lst.sort() |
[1, 2, 3] |
reverse() |
Reverses the order of the list | lst = [1, 2, 3] lst.reverse() |
[3, 2, 1] |
copy() |
Returns a shallow copy of the list | lst = [1, 2] new = lst.copy() |
new = [1, 2] |
In [25]:
Copied!
# List of cities
cities = [
"Kathmandu",
"New York",
"Tokyo",
"Paris",
"London",
"Sydney",
"Dubai",
"Toronto",
"Rome",
]
# List of cities
cities = [
"Kathmandu",
"New York",
"Tokyo",
"Paris",
"London",
"Sydney",
"Dubai",
"Toronto",
"Rome",
]
In [26]:
Copied!
cities.sort()
print(cities)
cities.sort()
print(cities)
['Dubai', 'Kathmandu', 'London', 'New York', 'Paris', 'Rome', 'Sydney', 'Tokyo', 'Toronto']
In [27]:
Copied!
cities.sort(reverse=True)
print(cities)
cities.sort(reverse=True)
print(cities)
['Toronto', 'Tokyo', 'Sydney', 'Rome', 'Paris', 'New York', 'London', 'Kathmandu', 'Dubai']
In [19]:
Copied!
removed_element = cities.pop(1)
print(removed_element)
removed_element = cities.pop(1)
print(removed_element)
New York
In [20]:
Copied!
print(cities)
print(cities)
['Kathmandu', 'Tokyo', 'Paris', 'London', 'Sydney', 'Dubai', 'Toronto', 'Rome']
In [12]:
Copied!
cities.append('Pokhara')
cities.append('Pokhara')
In [13]:
Copied!
print(cities)
print(cities)
['Kathmandu', 'New York', 'Tokyo', 'Paris', 'London', 'Sydney', 'Dubai', 'Toronto', 'Rome', 'Pokhara']
In [29]:
Copied!
# List of cities
cities = [
"Kathmandu",
"New York",
"Tokyo",
"Paris",
"London",
"Sydney",
"Dubai",
"Toronto",
"Rome",
]
print(cities.sort())
# List of cities
cities = [
"Kathmandu",
"New York",
"Tokyo",
"Paris",
"London",
"Sydney",
"Dubai",
"Toronto",
"Rome",
]
print(cities.sort())
None
In [30]:
Copied!
print(cities)
print(cities)
['Dubai', 'Kathmandu', 'London', 'New York', 'Paris', 'Rome', 'Sydney', 'Tokyo', 'Toronto']
In [31]:
Copied!
help(cities.sort)
help(cities.sort)
Help on built-in function sort: sort(*, key=None, reverse=False) method of builtins.list instance Sort the list in ascending order and return None. The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained). If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values. The reverse flag can be set to sort in descending order.
In [16]:
Copied!
## insert
a = [1, 2, 3, 5]
a.insert(5,1)
print(a)
## insert
a = [1, 2, 3, 5]
a.insert(5,1)
print(a)
[1, 2, 3, 5, 1]
In [17]:
Copied!
help(list.insert)
help(list.insert)
Help on method_descriptor: insert(self, index, object, /) unbound builtins.list method Insert object before index.
In [12]:
Copied!
cities.sort(reverse=True)
print(cities)
cities.sort(reverse=True)
print(cities)
['Toronto', 'Tokyo', 'Sydney', 'Rome', 'Pokhara', 'Paris', 'New York', 'London', 'Kathmandu', 'Dubai']
Exercise : Create list of atleast 7 favorite movies or series.¶
- Print the second and last movies.
- Print from second to second last movies
- Reverse the list.
- Add a new movie.
- Sort the list alphabetically.
- Remove the third movie
- clear all the list
Shallow Copy vs Deep Copy¶
A shallow copy creates a new outer object, but doesn't copy nested objects inside it.
Both the original and the copy share references to the same inner objects.
In [32]:
Copied!
original_list = [[1, 2], [3, 4], ['apple', 'mango', 'coconut']]
## shallow copy of the list
copy_list = original_list.copy()
print(original_list)
print(copy_list)
original_list = [[1, 2], [3, 4], ['apple', 'mango', 'coconut']]
## shallow copy of the list
copy_list = original_list.copy()
print(original_list)
print(copy_list)
[[1, 2], [3, 4], ['apple', 'mango', 'coconut']] [[1, 2], [3, 4], ['apple', 'mango', 'coconut']]
In [33]:
Copied!
copy_list[2][0]
copy_list[2][0]
Out[33]:
'apple'
In [34]:
Copied!
copy_list[0][0] = 5
print(original_list)
print(copy_list)
copy_list[0][0] = 5
print(original_list)
print(copy_list)
[[5, 2], [3, 4], ['apple', 'mango', 'coconut']] [[5, 2], [3, 4], ['apple', 'mango', 'coconut']]
In [35]:
Copied!
## an example of deep copy
import copy
original_list = [[1, 2], [3, 4]]
deep_copy_list = copy.deepcopy(original_list)
deep_copy_list[0][0] = 5
print(original_list)
print(deep_copy_list)
## an example of deep copy
import copy
original_list = [[1, 2], [3, 4]]
deep_copy_list = copy.deepcopy(original_list)
deep_copy_list[0][0] = 5
print(original_list)
print(deep_copy_list)
[[1, 2], [3, 4]] [[5, 2], [3, 4]]
In [ ]:
Copied!