Python LIST HowTo (Page: 1)
List in Python is a traditional array with a lot of methods of managing it. Every cell of list can have it’s own type.
How to remove first element from list in Python
There are few different ways to remove first element from list in Python.
1. removing element with list.pop() function
Function pop(i) will remove element number i and return it. If this element is not present – function will rise an error: IndexError
list = [1,2,3,4,5]
first = list.pop(0)
print(first) # 1
print(list) # [2,3,4,5]
2. removing element with del function
Function del(list[i]) will remove given element. If this element is not present – function will rise an error: IndexError
list = [1,2,3,4,5]
del(list[0])
print(list) # [2,3,4,5]
3. removing element with list.remove() function
Function list.remove(list[i]) will remove given element. If this element is not present – function will rise an error: IndexError
list = [1,2,3,4,5]
list.remove(list[0])
print(list) # [2,3,4,5]
4. removing element with slicing
slices will produce new list with subset of elements. Making this list from second element and assigning this new list to our list will do remove first element
list = [1,2,3,4,5]
list = list[1:]
print(list) # [2,3,4,5]
How to remove few element from list in Python
1. removing few element with del function
Function del(list[i:j]) will remove elements described in this slice. If some of those elements are not present – function will not do any action against absent elements
list = [1,2,3,4,5]
del(list[1:3])
print(list) # [1, 4, 5]
2. removing few element with slicing
Removing few elements with slicing is actually reverse task. We can leave untouched few elements with slicing.
list = [1,2,3,4,5]
list = list[1:4]
print(list) # [2,3,4] – only selected elements left in the list
So if you want to remove selected slice [1:4], you need to use concatenation of two parts
list = [1,2,3,4,5]
list = list[0:1]+list[4:]
print(list) # [2,3,4]
How to compare two lists
If you need to compare two lists in Python you have two different cases. What do you want to know – is the elements are the same in the list, or they are stored in the same order as well.
How to compare two lists for full identity
Use operator == to make full comparison of two lists
L1 = [1, 2, 3, 4]
L2 = [1, 3, 2, 4]
print(L1 == L2) # False
How to compare two lists for content only
If you are interesting to compare that two lists contains the same elements without comparing order, the easiest way is to compare sorted lists. Use operator == over the sorted() function
L1 = [1, 2, 3, 4]
L2 = [1, 3, 2, 4]
print(sorted(L1) == sorted(L2)) # True
How to iterate over list in Python
Iteration with range(len())
Traditional way for iterate through the list with indexes is to use range(len()) set of functions.
data = [1,-3,5,-4,8]
for idx in range(len(data)): # idx - index
print(idx, data[idx]) # data[idx] - list element for changing
Iteration over values in the list
For most of the common tasks it is easier to iterate over the values of the list. Yuo should understand, that val is a shallow copy of the value. Modification of val will not make any impact on the original list
data = [1,-3,5,-4,8]
for val in data: # val - value of each element
print(val, end=', ') # 1, -3, 5, -4, 8
val = 10
print( data ) # 1, -3, 5, -4, 8
Iteration with enumerate()
enumerate() is a very convenient build-in function, also preferable to iterate over the list in Python.
data = [1,-3,5,-4,8]
for idx, val in enumerate(data): # idx - index
data[idx] = 5; # val - list element for using
print(idx, data[idx], val) # data[idx] - list element for changing
Published: 2021-09-13 08:23:41
Updated: 2021-10-24 11:58:41