List in Python
List in Python
List in Python
A list is a standard data type of Python that can store a sequence of values belonging to a type. The Lists are depicted through square brackets
List can store a sequence of values belonging to a type. The Lists are depicted through square brackets
The data type list is an ordered sequence which is mutable and made up of one or more elements.
Elements of a list are enclosed in square brackets and are separated by comma.
List are mutable sequences of Python eg :- You can change the element of a list in place. Eg :-
[] :- list with no member, empty list #list of integers
[1, 2, 3] :- List of Integer
[1, 2.5, 3.7, 9] :- list of number(integers and float)
[‘One’, ‘Two’, ‘Three’] :- list of strings
Example :-
Characteristics of Lists
The list has the following characteristics:
The lists are ordered.
The element of the list can access by index.
The lists are the mutable type.
The lists are mutable types.
A list can store the number of various elements.
Creating list :-
To create a list, put a number of expressions in square brackets. That is, use square brackets indicate the start and end of the list, and separate the items by commas. For example:
[2, 4, 6]
Different ways to create list in python
The empty list :-
The empty list is []. It is the list equivalent of 0 or” and like them it also has truth value as false. You can also create an empty list
Lists are formed by placing comma-separated-list of expre ssions in square brackets
Example :-
The Long list :-
If a list contains many elements, then to enter such long lists, you can split it across several lines
Notice the opening square bracket and closing square brackets appear jus beginning and end of the list.
Example :-
The Nested list :-
A list can have an element in it, which itself is a list. Such a list is called nested list, e.g.. L * 1 = [3, 4, [5, 6], 7]
L1 is a nested list with four elements: 3, 4, [5, 6] and 7. L1[2] element is a list [5, 6]. Length of L1 is 4 as it counts [5, 6] as one element. Also, as L1[2] is a list (ie., [5, 6]). which means L1[2][0] will give 5 and L * 1[2][1] will give 6. We shall talk about nested lists in details in a later section.
Example :-
List are Similar to Strings in the fowing ways
1. Length :-
Function len(L) return the number of items (count) in the list L.
2. Index and Slicing :-
L[x] returns the item at index x (the first item has index 0) .
L[x:y] returns a new list, containing the objects at indexes between x and y (excluding index y)
3. Membership Operator :-
Both ‘in’ and ‘not in’ operator work on the Lists just like they work for other sequences.
4. Concatenation and repliation :-
The + operator add one list to the end of another.
The * operator repeats a list.
Accessing Elements in a List
Each element in list is accessed using value called index.
The fist index value is 0, the second index is 1 and so on. Elements in the list are assigned index values inincreasing order sterling from 0.
To access an element, use square brackets withthe index [ ] value of that element.
We may also use negative index value to access elements starting from the last element in the list, having index value -0.
Accessing Individual Elements
As mentioned, the individual elements of a list are accessed through their indexes.
name=[‘T’,’e’,’c’,’h’,’o’,’n’,’e’,’s’,’p’,’o’,’t’]
name[0]
‘T’
name[5]
‘n’
name[7]
‘s’
Example :-
Traversing a List
Traversal of a sequence means accessing and processing each element of it.
The for loop makes it easy to traverse or loop over the items in a list
We can access each element of the list or traverse a list using a for loop or a while loop.
Syntax :-
for <item> in <List>:
process each item her
Example :-
Comparing Lists :-
You can compare two lists using standard comparison operators of Python.This means that to compare equal, each corresponding element must compare equal and the two sequences must be of the same type
Example:-
Lists are Mutable
In Python, lists are mutable. It means that the contents of the list can be changed after it has been created.
List Operations
The data type list allows manipulation of its contents through various operations like :-
Concatenation
Repetition
Membership
Slicing
Concatenation :-
Python allows us to join two or more lists using concatenation operator using symbol +.
Concatenation is the merging of two or more values. Example: we can concatenate strings together.
Note that, there is no change in original lists It remain the same after concatenation operation. If we want to use the result of two concatenated lists, we should use an assignment operator.
Example:-
The concatenation operator ‘+’ requires that the operands should be of list type only. If we try to concatenate a list with elements of some other data type, TypeError occurs.Concatenation is the merging of two or more values.
Repetition
Python allows us to replicate the contents of a list using repetition operator depicted by symbol *.
Membership
The membership operator in checks if the element is present in the list and returns True, else returns False.
Example :-
The Operator not in transpose returns True if the element is not present in the list, else it returns False.
Example :-
Slicing :-
Slicing operations allow us to create new list by taking out elements from an existing list. List slices are the sub part of a list extracted out.
list=[start:stop].
list=[start:stop:step].
List Methods and Built-in Functions
Python also offers many built-in functions and methods for list manipulation. You have already worked with one such method len() in earlier chapters. In this chapter, you will learn about many other built-in powerful list methods of Python used for list manipulation.
Every list object that you create in Python is actually an instance of List class. The list manipulation methods that are being discussed below can be applied to lists.
list has several built-in methods
len()
list()
append()
extends()
insert()
count()
find()
remove()
pop()
reverse()
sort()
sorted()
min()
max()
sum()
Method
Description
Example
len()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
list()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
append()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
insert()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
count()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
find()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
remove()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
pop()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
reverse()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
sort()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
sorted()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
min()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
max()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
sum()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Popular Videos
UX for Teams
Learn the basics and a bit beyond to improve your backend dev skills.
Designer
SEO & Instagram
Learn the basics and a bit beyond to improve your backend dev skills.
Designer
len()
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.