Table of Contents:
- How to create a list?
- List Comprehension: Elegant way to create new List
- List Length/size
- How to add elements to a list?
- How to access elements from a list?
- a. List Index
- b. List negative indexing
- c. How to slice lists in Python?
- d. Check if Item Exists in List
- How to delete or remove elements from a list?
- a. List remove
- b. List pop
- c. List del
- Other List Operations in Python
- a. List Membership Test
- b. Iterating Through a List
- Python List Methods
- a. List append()
- b. List extend()
- c. List insert()
- d. List remove()
- e. List pop()
- f. List clear()
- g. List index()
- h. List count()
- i. List sort()
- j. List reverse()
- k. List copy()
- Built-in functions with List
- a. reduce()
- b. sum()
- c. ord()
- d. cmp()
- e. min()
- f. all()
- g. any()
- h. len()
- i. enumerate()
- j. accumulate()
- k. filter()
- l. map()
- m. lambda()
How to create a list?
A list is created by placing all the items (elements) inside square brackets , separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.). It can also have another list as an item. This is called a nested list.
List Comprehension: Elegant way to create new List
A list comprehension is an elegant, concise way to define and create a list in Python. Python List Comprehensions consist of square brackets containing an expression, which is executed for each element in an iterable. Each element can be conditionally included and or transformed by the comprehension.
Create a new list with for loop:
Create a new list with list comprehension:
Python list method len() returns the number of elements in the list.
How to add elements to a list?
Add an element to a list:
How to access elements from a list?
a. List Index
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.
b. List negative indexing
The negative indexing starts from where the array ends. This means that the last element of the array is the first element in the negative indexing which is -1.
c. How to slice lists in Python?
In a slicing, the start position
start and end position
stop of the selection
are written as
start <= x <stop is selected. Note that the item at
included, but the item at
stop is not included.
d. Check if Item Exists in List
To check if Python list contains a specific item, use an inbuilt
in operator. The
in operator that checks if the list contains a specific element or not.
It can also check if the item exists on the list or not using
any function. But
in operator is most common use.
Using for loop
Using list count (reference to
count builtin function with list)
Using any (reference to
any builtin function with list)
How to delete or remove elements from a list?
a. List remove
The remove() method is one of the most commonly used list object methods to remove an item or an element from the list. When you use this method, you will need to specify the particular item that is to be removed.
b. List pop
When using the pop(), we specify the index of the item as the argument, and hence, it pops out the item to be returned at the specified index.
c. List del
The del operator removes the item or an element at the specified index location from the list, but the removed item is not returned, as it is with the pop() method.
Other List Operations in Python
a. List Membership Test
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators
In - Evaluates to true if it finds a variable in the specified sequence and false otherwise
Not in - Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
b. Iterating Through a List
Using For loop
Python List Methods
a. List append()
Add an element to the end of the list
b. List extend()
Add all elements of a list to the another list
c. List insert()
Insert an item at the defined index
d. List remove()
Removes an item from the list
e. List pop()
Removes and returns an element at the given index
f. List clear()
Removes all items from the list
Note: If you are using Python 2 or Python 3.2 and below, you cannot use the clear() method. You can use the del operator instead.
g. List index()
Returns the index of the first matched item
h. List count()
Returns the count of number of items passed as an argument
i. List sort()
Sort items in a list in ascending order
Sorting list of Numbers
Sorting list of Strings
j. List reverse()
Reverse the order of items in the list
k. List copy()
Returns a shallow copy of the list
Copy() method is not available in python 2.
Built-in functions with List
Apply a particular function passed in its argument to all of the list elements stores the intermediate result and only returns the final summation value
Create Built-in sum() function
Using Operator Function
Sums up the numbers in the list
Returns an integer representing the Unicode code point of the given Unicode character
This function returns 1, if first list is “greater” than second list max() return maximum element of given list in Python 2.x
cmp() does not work in Python 3.x
Return minimum element of given list
Returns true if all element are true or if list is empty
The 0 (zero) values in lists are considered as false and the non-zero values such as 1, 20 etc are considered true.
Return true if any element of the list is true. If list is empty, return false
Returns length of the list or size of the list
Returns enumerate object of list
Apply a particular function passed in its argument to all of the list elements returns a list containing the intermediate results
This method is not available in python 2.
Tests if each element of a list true or not
Returns a list of the results after applying the given function to each item of a given iterable
This function can have any number of arguments but only one expression, which is evaluated and returned.