Python lists are mutable, ordered collections of items starting from 0. Objects in this situation can range from integers to strings. Lists can include other lists.
In this post, we will look at the fundamentals of lists in Python, as well as ways for interacting with them.
When a list is created, an empty place in memory is reserved. On the one hand, this is no different from constructing any other data type; yet, the contents of the list can change:
numbers = [1, 2] numbers = 3 # updated list: [1, 3]
It will print 2 before replacing the element of the sequence
print(numbers), and 3 after the replacement.
Creating a List in Python
This may be done in a number of ways, such as by enumerating the members of a list in square brackets:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
In this example, the unit will be at position 0, that is, it print(numbers)will display 1.
A function list can also be used to process an iterable object () by a function list().. So, let's get some string:
list('anurag') # ['a', 'n', 'u', 'r', 'a', 'g']
List comprehensions enable you to apply a specified phrase to each member in a sequence. Assume you need to make a list of numbers ranging from 1 to 5 inclusive:
numbers = [i for i in range(1,6)] # [1, 2, 3, 4, 5]
You may obtain a subset of values by slicing. The following code will provide a list of items beginning at index 0 and ending at index 2 or higher:
numbers = [1, 5, 9, 6] print(numbers[0:2]) # result [1, 5]
After that, print everything except the element at position 3:
print(numbers[:3]) # result [1, 5, 9]
And now, beginning with index 1 and working your way to the end:
print(numbers[1:]) # result [5, 9, 6]
Python List Operations
x in l—
trueif the element
xis in the list
x not in l—
trueif the element
xis not present in
l1 + l2— union of two lists;
l * n , n * l- copies the list of
len(l)is the number of elements in
min(l)is the smallest element;
max(l)is the largest element;
sum(l)is the sum of the numbers in the list;
for i in list()iterates over elements from left to right.
Python List Methods
Returns the location of the first matched element. The search for a match is done from left to right. Example:
numbers = [1, 5, 9, 6, 1, 2, 1] print(numbers.index(1)) # output 0: initial unit discovered at location 0
This function counts the number of occurrences of the supplied value in the Python list:
numbers = [1, 5, 9, 6, 1, 2, 1] print(numbers.count(1)) # output 3, because the unit occurs 3 times
Adds the specified value to the end:
numbers = [1, 5, 9, 6] numbers.append(3) # updated list: [1, 5, 9, 6, 3]
In Python, this function sorts a list. From smallest to largest by default:
numbers = [1, 5, 9, 6] numbers.sort() # updated list: [1, 5, 6, 9]
A series of components can alternatively be sorted from largest to smallest:
numbers = [1, 5, 9, 6] numbers.sort(reverse = true) # updated list: [9, 6, 5, 1]
The element is inserted before the specified index:
numbers = [1, 5, 9, 6] numbers.insert(3, [2, 3]) # updated list: [1, 5, 9, [2, 3], 6]
Removes the first occurrence of a Python list element:
numbers = [1, 5, 9, 6, 1, 2, 1] numbers.remove(1) # updated list: [5, 9, 6, 1, 2, 1]
The function extend(), like the method append(), adds items, but it also allows you to add lists:
numbers = [1, 5, 9, 6] numbers.extend([2, 3]) # updated list: [1, 5, 9, 6, 2, 3]
This method additionally displays the deleted element when removing the element at the provided index. If no index is specified, the last element will be removed:
numbers = [1, 5, 9, 6] numbers.pop(1) # we get: # 5 # [1, 9, 6]
This function converts a list to a string. The element separator is included in quotation marks before the method, and the Python list itself must be made up of strings:
mylist = ['site', 'typical', 'programmer'] print(', '.join(mylist)) # output 'site, typical, programmer'
Will share some new tips in next article, thanks!