Lists are Non-Primitive data structure in python. Lists are the ordered collection of data. It is changeable.
List contains different Primitive Data types like String, Integer, Float and Boolean. So Let’s create list. List is enclosed with square brackets.
At the end you will get a FREE cheat sheet on python lists.
#Creating list in python list = ["Hello World",100,5.2,True] print(type(list)) #Output <class 'list'>
#List of numbers including integers and floats number_list = [10,29,53.2,500.20] print(number_list) #Output is [10,29,53.2,500.20]
So this is how you can create list in python.
Sometimes there is no item in list that is called empty list. Like,
#Empty list empty_list =  print(empty_list) #Output is 
How To Access Elements Of List
In python lists index starts with 0. It means if you need to access the first element or item of the list then you have to put 0 in square bracket. like list_name to access the first element of the list.
Have a look at Example,
#This is our list list = [1,2,3,4,5] #Now I want to print first element of list (which is 1) print(list) #Output 1
This is how you can access elements from the list. If you need to access second element (which is 2) then you have to type :
print(list) #Output 2
You can also access elements in a reverse manner. Like, If you need to access last element (which is 5) of the list then your code will be :
list = [1,2,3,4,5] print(list[-1]) #Output 5
Add Element In List
You can also add element at last position in list. By using append() method.
have a look at Example,
#Initially created list list = [1,2,3,4,5] #Now let's add 6 in the list list.append(6) print(list) #Output [1, 2, 3, 4, 5, 6]
Remove Element From List
Recently we added element in list. same like that we can also remove element from list. By pop() method
#Initially created list list = [1,2,3,4,5,6] #Now let's remove 6 from the list list.pop() print(list) #Output [1, 2, 3, 4, 5]
List Slicing or Range Of Indexes
When you need to access certain elements from a list (more than one) then list slicing come to action. Have a look at how you can do list slicing.
#This is our initial list a = [1,2,3,4,5,6,7,8,9,10] #Now if you want to access 3 to 7 numbers print(a[2:7]) #Output [3, 4, 5, 6, 7]
Here we put 2 because in list index starts with 0 and at the end we put 7 because, in list slicing last element will excluded.
Which means last element will not be printed. Like this you can access more than one element using this list slicing.
There are some amazing methods for accessing elements.
#Initial list a = [1,2,3,4,5,6,7,8,9,10] #Now if you want to access 1 to 5 print(a[:5]) #Output [1, 2, 3, 4, 5]
This is also a valid way to access elements from beginning of list. If we don’t put any number before colon(:) then python will start tacking elements from 0th index.
You can also use this way to access whole list:
#Initial list a = [1,2,3,4,5,6,7,8,9,10] #Now you want to access whole list print(a[:]) #Output [1,2,3,4,5,6,7,8,9,10]
Change Item Value
We can easily change value of list’s item.
In the below example we will change “Java” to “Python”
#This is our list languages = ["C","C++","Java","HTML"] #Index of java is 2 because index starts with 0 #Now we use item's index to change the value languages = "Python" print(languages) #Output ['C', 'C++', 'Python', 'HTML']
This is the great way to change value of item.
Add Item At Specific Position
In python lists we can add any value at any position (If index exist). By using insert() function.
insert() is a built-in function in python. insert function takes two arguments. index and value. insert(index,value)
Try this Example,
#Initial list a = [1,2,3,5] #Now you want to add 4 (Index will be 3) a.insert(3,4) print(a) #Output [1, 2, 3, 4, 5]
Delete Any Item From List
We will use del keyword to delete any specific item from a list.
#Initial List languages = ["C","C++","Python","HTML"] #Now we want to remove "HTML" #Index of "HTML" is 3 or -1 del languages[-1] print(languages) #Output ['C', 'C++', 'Python']
Combine Two Lists
We recently used append() function to add item to the list. same like that we can also append second list to first one.
#We have two lists here a = [1,2,3] b = [4,5] #Now we want to append second list(b) to the first one(a) a.append(b) print(b) #Output [1, 2, 3, [4, 5]]
Sort List Items In Python
Think if we have not arranged numbers then we can use sort method to arrange number in traditional manner.
We can sort data by sort() function. Which is in-built function in python
#Our initial list a = [1,5,3,4,2] #Let's sort this list a.sort() print(a) #Output [1, 2, 3, 4, 5]
This is all about python lists.
In starting i told, you will get a cheat sheet of python lists. HERE IT IS