In this tutorial, we will learn about the List in Python. Its types, and how to use them with the help of examples.
List is use to store the group of values. A list can be compose by storing a sequence of different type of values separated by commas. List is mutable object so we can do the manipulations. A list is enclose between square [] brackets. In list insertion order is preserve it means in which order we insert element same order output is print. A List duplicate objects are allowed. The list contains forward indexing & backward indexing.
The Creates a Lists Python
Example of lists in python# Creating a List List = [] print(List) # Creating a list of strings List = ['Basic Engineer', 'Student'] print(List) # Creating a Multi-Dimensional List List = [['Basic', 'Engineer'], ['Hello Student']] print(List)
Let’s run the code
Accessing Elements From The List
Accessing elements from the List# single-Dimensional List List1 = ['Basic', 'Engineer', 'Student'] print("single-demensional list") print(List1[0]) print(List1[2]) # Multi-Dimensional List List2 = [['Basic', 'Engineer', 'Student'], ['Hello','Student']] print("Multi-Dimensional list") print(List2[0][2]) print(List2[1][1]) # negative list List3 = [1, 2, 'Basic', 4, 'Engineer', 6, 'Student'] print("negative") print(List3[-2]) print(List3[-7])
Let’s run the code
Python Lists Operations
- Repetition
- Symbol of Repetition is a *
- Concatenation
- Symbol of Concatenation +
- Length
- Iteration
- Membership
The Example of Python Lists Operations
Example of Python Lists Operations# repetition list1 = [2, 4, 6, 8, 10, 12, 14] a = list1 * 2 print(a) # concatenation list2 = [2, 4, 6, 8, 10, 12, 14] list3 = [3, 6, 9, 12, 15, 18, 21] a = list2 + list3 print(a) # lenght list4 = [2, 4, 6, 8, 10, 12, 14] len(list4) # iterating list5 = [2, 4, 6] for i in list5: print(i) # membership list6 = [2, 4, 6, 8, 10, 12, 14] print(6 in list6) print(7 in list6) print(10 in list6)
Let’s run the code
Method of Lists in Python
Method | Description |
append() | add an item to the end of the list |
extend() | add items of lists and other iterables to the end of the list |
insert() | inserts an item at the specified index |
remove() | removes item present at the given index |
pop() | returns and removes item present at the given index |
clear() | removes all items from the list |
index() | returns the index of the first matched item |
count() | returns the count of the specified item in the list |
sort() | sort the list in ascending/descending order |
reverse() | reverses the item of the list |
copy() | returns the shallow copy of the list |
Slicing of Python Lists
It is possible to access a section of items from the list using the slicing operator :
Syntax
Syntax of Slicing of Python Listslist_varible(start:stop:step) Where, The start denotes the starting index position of the list. The stop denotes the last index position of the list. The step is use to skip the nth element within a start:stop
A Example of Slicing of Python Lists
Example of Slicing of Python Lists# slicing list7 = ['b','a','s','i','c','e','n','g','i','n','e','e','r', 1,2,3] print(list7[8:15]) print(list7[9:]) print(list7[:])
Let’s run the code
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about Python Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments