The List is a type of data container in Python which is used to store multiple data in one variable. It can contain elements of different data types. Elements in a list are ordered which can be accessed using index number.
List can be created by separating it's elements using comma (,) and enclosing them with square bracket [ ]. Additionally, it can also be created using and Next Page.
#List with multiple datatypes Info = ['John', 25, 'London'] print(Info) #Creating list using list function colors = list(('Red', 'Blue', 'Green')) print(colors) #Creating list using list comprehension x = [1, 2, 3, 4, 5] y = [i*i for i in x] print(y)
['John', 25, 'London'] ['Red', 'Blue', 'Green'] [1, 4, 9, 16, 25]
An element of a list can be accessed with it's index number. In Python, index number starts with 0 in forward direction and -1 in backward direction. The below figure and example describe the indexing concept of a list.
weekday = ['MON', 'TUE', 'WED', 'THU', 'FRI'] #forward indexing print(weekday[1]) #backward indexing print(weekday[-1])
TUE FRI
Range of elements of a list can be selected using statement like [start_index : end_index] where end_index is excluded. If start_index and end_index are not mentioned then it takes first and last index numbers of the list respectively.
weekday = ['MON', 'TUE', 'WED', 'THU', 'FRI'] print(weekday[1:3]) print(weekday[-5:-1]) print(weekday[1:]) print(weekday[:-3]) print(weekday[:])
['TUE', 'WED'] ['MON', 'TUE', 'WED', 'THU'] ['TUE', 'WED', 'THU', 'FRI'] ['MON', 'TUE'] ['MON', 'TUE', 'WED', 'THU', 'FRI']
To change element's value, assign new value using it's index.
Info = ['John', 25, 'London'] #value at index=0 changed to 'Marry' Info[0] = 'Marry' print(Info)
['Marry', 25, 'London']
Below mentioned methods are used to add elements in a list:
days = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'] # add this element in last of the list days.append('SUN') print(days) month = ['JAN', 'FEB', 'MAR', 'MAY'] # add 'APR' at index=3 of the list month.insert(3,'APR') print(month)
['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', SUN'] ['JAN', 'FEB', 'MAR', 'APR', 'MAY']
Below mentioned methods/keywords is used to delete elements from a list:
number = [10, 50, 50, 100, 1000] #delete first occurrence of 50. number.remove(50) print(number) number = [10, 50, 50, 100, 1000] #delete element at index=3. number.pop(3) print(number) number = [10, 50, 50, 100, 1000] #delete last element from the list. number.pop() print(number) number = [10, 50, 50, 100, 1000] #delete all elements from the list. number.clear() print(number)
[10, 50, 100, 1000] [10, 50, 50, 1000] [10, 50, 50, 100] []
The del keyword is used to delete an element or range of elements or list itself.
number = [10, 50, 50, 100, 1000] #delete element at index=3. del number[3] print(number) number = [10, 50, 50, 100, 1000] #delete element at index=1,2 and 3. del number[1:4] print(number) number = [10, 50, 50, 100, 1000] #delete list 'number'. del number print(number)
[10, 50, 50, 1000, 1000] [10, 1000, 1000] NameError: name 'number' is not defined
The len() function can be used to find out total number of elements in a list, tuple, set or dictionary.
number = [10, 50, 50, 100, 1000, 1000] print(len(number))
6
for loop can be used to access each element of a list.
colors = ['Red', 'Blue', 'Green'] for x in colors: print(x)
Red Blue Green
By using while loop and len() function, each element of a list can be accessed.
colors = ['Red', 'Blue', 'Green'] i = 0 while i < len(colors): print(colors[i]) i = i + 1
Red Blue Green
If control statement is used to check whether the list contains specified element or not.
colors = ['Red', 'Blue', 'Green'] if 'white' in colors: print('Yes, white is an element of colors.') else: print('No, white is not an element of colors.')
No, white is not an element of colors.
There are several ways to create a copy of the list.
colors = ['Red', 'Blue', 'Green'] mycolor = colors yourcolor = colors.copy() hiscolor = list(colors) print(mycolor) print(yourcolor) print(hiscolor) #delete last element in 'colors' colors.pop() print(mycolor) print(yourcolor) print(hiscolor)
['Red', 'Blue', 'Green'] ['Red', 'Blue', 'Green'] ['Red', 'Blue', 'Green'] ['Red', 'Blue'] ['Red', 'Blue', 'Green'] ['Red', 'Blue', 'Green']
There are several ways to join lists.
colors = ['Red', 'Blue', 'Green'] numbers = [10, 20] mylist1 = colors + numbers print(mylist1) colors = ['Red', 'Blue', 'Green'] numbers = [10, 20] for i in numbers: colors.append(i) print(colors) colors = ['Red', 'Blue', 'Green'] numbers = [10, 20] colors.extend(numbers) print(colors)
['Red', 'Blue', 'Green', 10, 20] ['Red', 'Blue', 'Green', 10, 20] ['Red', 'Blue', 'Green', 10, 20]
Methods | Description |
---|---|
Adds a specified element to the end of the list | |
Create List | Deletes all elements from the list |
Creates a copy of the list into a new list | |
List can be created by separating it's elements using comma (,) and enclosing them with square bracket [ ]. Additionally, it can also be created using | Returns the number of occurrence of a specified element in the list |
list() function | Adds an element or iterable at the end of the list |
and | Returns the index of first occurrence of a specified element in the list |
list comprehension | Adds a specified element at specified index in the list |
. | Deletes an element at specified index or last element of the list |
Deletes first occurrence of a specified element in the list | |
Reverses order of all elements in the list | |
Example | Sorts elements of the list in ascending or descending order with specified sorting criteria |
Functions | Description |
Returns total number of elements in the list | |
#List with multiple datatypes Info = ['John', 25, 'London'] print(Info) #Creating list using list function colors = list(('Red', 'Blue', 'Green')) print(colors) #Creating list using list comprehension x = [1, 2, 3, 4, 5] y = [i*i for i in x] print(y) | List function/constructor is used to create list from iterable like list, tuple, set, string and dictionary, etc. |