List Data Type in Python
List
Lists in Python are an ordered collection of different data types,the items in the list are separated by commas (,) and delimited by opening and closing square brackets.In this article we will be see about
- Data View
- Data Insertion
- Data Delete
- Data Updation
Data Visualize
Creating Empty List: Now we will create the empty list .
Python 3.8.4 (tags/v3.8.2:7b3acdb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 = >>> data = [] >>> print(data) [] >>> print(type(data))>>> print(len(data)) 0
Creating Non-Empty List: Now we will create the non-empty list
Python 3.8.4 (tags/v3.8.2:7b3acdb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = [101,'ankita',95.6,('P','C','M'),[96,91,93]] >>> print(data) [101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]] >>> print(type(data))>>> print(len(data))
List Indexing:
This is indexing of the given demonstration along with the syntax.
Syntax: object[index]
Backward Indexing | -5 | -4 | -3 | -2 | -1 |
101 | ankita’ | 95.6 | (‘P’,’C’,’M’) | [96, 91, 93] | |
Forward Indexing | 0 | 1 | 2 | 3 | 4 |
Python 3.8.4 (tags/v3.8.2:7b3acdb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = [101,'ankita',95.6,('P','C','M'),[96,91,93]] >>> print(data) [101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]] >>> print(data[3]) ('P', 'C', 'M') >>> print(data[-1]) [96, 91, 93]
List Index slicing:
Slicing is used to see the customized data here we give the demonstration along with syntax.
Syntax: object[start : stop : stride(step)]
start (optional) | starting index value, the default is 0 (in fwd indexing) |
stop (optional) | ending index value, is not included in the generated sequence, default is len(object)(in fwd indexing) |
step (optional) | the difference between each item in the sequence, the default is 1(in fwd indexing) |
Python 3.8.4 (tags/v3.8.2:7b3acdb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = [101,'ankita',95.6,('P','C','M'),[96,91,93]] >>> print(data) [101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]] >>> print(id(data)) 2300730249680 >>> print(data[::]) [101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]] >>> print(data[2:]) [95.6, ('P', 'C', 'M'), [96, 91, 93]] >>> print(data[2:4]) [95.6, ('P', 'C', 'M')] >>> print(data[:4]) [101, 'ankita', 95.6, ('P', 'C', 'M')] >>> print(data[-4:-1]) ['ankita', 95.6, ('P', 'C', 'M')] >>> print(data[-1::-1]) [[96, 91, 93], ('P', 'C', 'M'), 95.6, 'ankita', 101] >>> print(id(data[-4:-1]) 2300730249680
Data Insertion
In the heading will be learning about data insertion in the list,there python gives the some in-built function which is used to insert data in a new or existing list.
List Insertion Method
Function | Description |
append(object) |
|
extend(iterable) | extend are used to insert multiple values in the list. |
insert(index, object) | insert are used to add elements at specific index values. |
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> a=[1,2,"salesforcedrillers",23.33,True] >>> a.append("using_append") >>> a [1, 2, 'salesforcedrillers', 23.33, True, 'using_append'] >>> a.append(["create_nested_list",234,33,"salesforcedrillers"]) >>> print(a) [1, 2, 'salesforcedrillers', 23.33, True, 'using_append', ['create_nested_list', 234, 33, 'salesforcedrillers']] >>> a.extend([343,4,43]) >>> print(a) [1, 2, 'salesforcedrillers', 23.33, True, 'using_append', ['create_nested_list', 234, 33, 'salesforcedrillers'], 343, 4, 43] >>> a.insert(2,"Using_Insert") >>> a [1, 2, 'Using_Insert', 'salesforcedrillers', 23.33, True, 'using_append', ['create_nested_list', 234, 33, 'salesforcedrillers'], 343, 4, 43]
Data Deletion
Deletion is used to remove any element from the list here python provides some in-built function to delete the value from the list. Before working with the program now will we see the in-built function.
List Deletion Method
Function | Description |
del list_name | It is delete all the list along with the element |
clear() | It deletes all the elements but lists exist. |
pop() |
|
remove() | It is delete value by taking the value |
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> a=[1,2,3,4,5,6,7,8,9,10] >>> a.remove(2) >>> a [1, 3, 4, 5, 6, 7, 8, 9, 10] >>> a.pop() 10 >>> print(a) [1, 3, 4, 5, 6, 7, 8, 9] >>> a.pop(2) 4 >>> print(a) [1, 3, 5, 6, 7, 8, 9] >>> a.clear() >>> print(a) [] >>> del a >>> print(a) Traceback (most recent call last): File "", line 1, in print(a) NameError: name 'a' is not defined >>>
Data Updation
Data updation is used for the update any value in the exist list,you can replace any value by using following method :-
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a=[1,2,3,4,5,6,7] >>> a[3] 4 >>> a[3]="salesforcedrillers" >>> a [1, 2, 3, 'salesforcedrillers', 5, 6, 7
Working with Additional Feature
This chapter will deal with some extra features of the python and give some demonstration.
List Extra Method
Function | Description |
reverse() | It is used for the reverse element of the list. |
sort() | It is used to sort the list in ascending order. |
count() | It is used for counting the elements of the list. |
copy() | It is used for returning the copy of the list. |
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> list_a=[1,45,4523,5,352,6,54,75365,425,66,43,67,444,42,2,54,] >>> >>> print(list_a) [1, 45, 4523, 5, 352, 6, 54, 75365, 425, 66, 43, 67, 444, 42, 2, 54] >>> list_a.reverse() >>> print(list_a) [54, 2, 42, 444, 67, 43, 66, 425, 75365, 54, 6, 352, 5, 4523, 45, 1] >>> list_a.sort() >>> print(list_a) [1, 2, 5, 6, 42, 43, 45, 54, 54, 66, 67, 352, 425, 444, 4523, 75365] >>> list_a.sort(reverse=True) >>> print(list_a) [75365, 4523, 444, 425, 352, 67, 66, 54, 54, 45, 43, 42, 6, 5, 2, 1] >>> list_a.count(43) 1 >>> list_b=list_a.copy() >>> print(list_b) [75365, 4523, 444, 425, 352, 67, 66, 54, 54, 45, 43, 42, 6, 5, 2, 1] >>>
Working with In-built function
Here Python provides the inbuilt function to make computation more easy then before ,just invoking some basic functions we will solve the complex mathematical questions.
List In-Built Method
sum() | Used to add all the elements of the list. |
ord() | Used for finding the unicode of any element. |
max() | Used for finding the biggest number of the list. |
min() | Used for finding the lowest number of the list. |
all() | Return the true value if all values will be true,perform as and operations |
any() | Return the true if any value of true ,perform as or operation. |
len() | Return the length of the list. |
enumerate() | Return the all index value of the every element of the list. |
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> list_a=[2,42342,34,23,42,42,34,2,41,41,234,] >>> sum(list_a) 42837 >>> ord("a") 97 >>> max(list_a) 42342 >>> min(list_a) 2 >>> len(list_a) 11 >>> list(enumerate(list_a)) [(0, 2), (1, 42342), (2, 34), (3, 23), (4, 42), (5, 42), (6, 34), (7, 2), (8, 41), (9, 41), (10, 234)] >>> another_list=[True,True,False,True] >>> any(another_list) True >>> all(another_list) False >>>
List Comprehension
List comprehension provides an elegant way to create the list,with the help of comprehension we get maximum output in less code.
Syntax :
list_name=[output exp for variable in input_list if(any condition)]
Example 1 Writes the program to print form 1 to 100 numbers
>>> num=[num for num in range(101)] >>> print(num) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] >>> type(num)>>>
Example 2 writes the program to split “salesforcedrillers” strings.
>>> splitting=[string for string in "salesforcedrillers"] >>> print(splitting) ['s', 'a', 'l', 'e', 's', 'f', 'o', 'r', 'c', 'e', 'd', 'r', 'i', 'l', 'l', 'e', 'r', 's'] >>>
Example 3 writes the program to find the even number from 1 to 50 number.
>>> even=[num for num in range(1,51)if(num%2==0)] >>> print(even) [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50] >>>
Example 4 finding the square of all even number from 1 to 50
>>> square=[num*num for num in range(1,50)if(num%2==0)] >>> square [4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 2304]