Python List
Python List Data Type
Python List Data Type can be in order when you store data. New item is added to list, will be located at the end of list.Lists are data collection which can store multiple in a single value.List Type are mutable, meaning you can modify, add, and remove elements after the list is created.
List Type are denoted by square brackets []
and elements within the list are separated by commas.
– List Data Type can be changeable after we’ve already created such as add , edit, delete etc..
There are many methods of Python list such as
- append (x) => add item to the end of list
- extend (iterable) => add the whole element from one list to another list.
- iterable can be list,tuple, set ..etc.
- insert (position, x) => insert item to list by position you want in the list.
- remove (x) => remove the first item in the list by specific value
- pop (x) => remove item by the position that you specify
- clear() => remove all item in list.
- index() => return the index of the item in list as number. Where the position of item is.
- count() => to count how many time of element in list that is appear. Return as number.
- sort() => use to sort ascending in list. it is a default sorting (ascending)
For example:
How to declare List in Python.
myList=["Toyota",'Mitsubshi','Volvo']
print(myList)
#Output: ['Toyota', 'Mitsubshi', 'Volvo']
List declaration is use Square Brackets, not Curly Braces. Don’t confuse with Set data type.
I will introduces Set data type in the next post.
For example:
How to Add item in List my using method append(x)
myList=["Toyota",'Mitsubshi','Volvo']
myList.append("Ford")
print(myList)
#Output: ['Toyota', 'Mitsubshi', 'Volvo', 'Ford']
Append method will add ‘Ford’ item to the and of list.
you will see the output on the screen short that I showed you .
For example:
How to use extend (iterable)
myCar1=["Toyota",'Mitsubshi','Volvo']
myCar2=["Honda","Mazda","Lexus"]
myCar1.extend(myCar2)
print(myCar1)
#Output: ['Toyota', 'Mitsubshi', 'Volvo', 'Honda', 'Mazda', 'Lexus']
myCar1 will extend with all element from myCar2.
For example:
How to use insert(position, i)
myCar1=["Toyota",'Mitsubshi','Volvo']
myCar2="Honda"
i=1#postion
myCar1.insert(i,myCar2)
print(myCar1)
#Output: ['Toyota', 'Honda', 'Mitsubshi', 'Volvo']
‘Honda’ will insertto list by index 1. Index means the position in list which can start from 0,1,…..i.
For example:
How to use remove(x)
myCar=["Toyota",'Mitsubshi','Volvo']
myCar.remove(myCar[0])
print(myCar)
#Output: ['Mitsubshi', 'Volvo']
‘Toyota‘ was removed from the list because myCar[0] =’Toyota’.
If there is no item in list, it will get ValueError.
For example:
How to use pop(x)
myCar=["Toyota",'Mitsubshi','Volvo']
myCar.pop(2)
print(myCar)
#Output: ['Toyota', 'Mitsubshi']
‘Volvo’ was removed from list because of pop(2)=’Volvo’, however, if you pop(x), x is out of range of the list, it will get an error message IndexError.
For example:
How to use clear()
myCar=["Toyota",'Mitsubshi','Volvo']
myCar.clear()
print(myCar)
#Output: []
For example:
How to use index(argument), count(),
index the element in list which is return number of the position of elements.
count how many times of elements that are appearing in list.
myCar=["Toyota",'Mitsubishi','Volvo']
x=myCar.index('Mitsubishi')
print(x)
#Output: 1
#----------------
#count()
myCar=['Toyota','Mitsubishi','Volvo','Toyota']
x=myCar.count('Toyota')
print(x)
#Output: 2
For example:
How to use sort()
myCar=["Toyota",'Mitsubishi','Volvo']
myCar.sort(reverse=True)
print(myCar)
#Output: ['Volvo', 'Toyota', 'Mitsubishi']
For Example
How to use copy()
myCar=["Toyota",'Mitsubishi','Volvo']
myCar=myCar.copy()
print(myCar)
#Output: ['Toyota', 'Mitsubishi', 'Volvo']