Professional Documents
Culture Documents
1. .count ():
A list method to count the number of occurrences of an element in a list.
For example, we want to find how many times a word or integer appeared
in a list can be found by: using list_name.count(“item_name” or number)
2. .insert ( ):
A list method to insert an element into a specific index of a list.
Comment: (Previously, we studied that to add something to a list we use amend.() or
addition method but that method will only add the item at the end of the list. If we want
to add an item at a certain index we need to use .insert().
Syntax for .insert( ) is:
3. .pop():
A list method to remove an element from a specific index or from the end
of a list.
Comment: (Previously, we used .remove() to remove an item from the list but that
requires the item name or value, what if we want to remove the item using its index. So
we use .pop() to remove an item existing at that index. If the space between parenthesis
is left empty the last item [-1] index, will get removed automatically.
list_name = range (0:10) # 10 will not be included, starting number can be changed.
list_name = range (21) # so if we want to print 0 to 20. We put range 21.
To print these numbers we will use print but with another syntax list.
print(list(list_name))
we can also print sequences using the range, for that we use the following syntax:
list_name = range( x, y , z )
where x = initial value (it will be included in the range),
y = ending value (it will not be included in the range but all numbers before will)
z = common difference between x and y.
5. len() :
A built-in Python function to get the length of a list, we can find the length
of any list and store or print it using the syntax:
length_of_list = len(list_name)
print(len(list_name)
6. Sort ( )/ Sorted()
A method and built-in function to sort a list. There are two methods
sorted_list = list_name.sort() # you can use reverse=True in ( ) for backwards
sorted_list = sorted(list_name)
SLICING A LIST:
In Python, often we want to extract only a portion of a list. Dividing a list in such a
manner is referred to as slicing.
The syntax for slicing a list is:
Syntax :
Combine_list = zip(list_1,list_2)
Print(list(Combine_list))