✨Collections In Python :-List✨
— — — — — — — — — — — — — — — — — — — — — — — — — — — —
Contents of this blog:
- collection data types which are supported by Python
- List
- Defining a List
- List Indexing
- Accessing the List element
- Concatenation of Two list
- List Iteration
- List built-in function
— — — — — — — — — — — — — — — — — — — — — — — — — — —
There are many collection data types which are supported by Python.
- List
- Tuple
- String
- Set
- Dictionary
List :
List can be used to store a group of elements together in a sequence.
list stores a series of items in a particular order. You access items using an index.
Real life Example :
You can use a List to store the ingredients to cook a chicken, because Lists support sequential access and you can access the steps in order.
Defining a List :
Use square brackets( [] )to define a list, and use commas(,) to separate individual items in the list.
Making a list
users = [“shruti”,“vishal”, “bobby”, “rony”, “kamal”]
List Indexing :
- Each element in the list has a position in the list known as an index.
- The list index starts from zero.
Creating a Empty List :
>>> empty_list = []
>>> empty_list = list()
- Create a list with Known element & Known size :
>>> sample_list = [1,2,3,4,5]
- Create a list with Known element & Unknown size :
>>> sample_list = [None]*5
|
|
----> None denotes an unknown value.
Accessing the List element :
- Individual elements in a list are accessed according to their position, called the index. The index of the first element is 0, the index of the second element is 1, and so forth.
- Negative indices refer to items at the end of the list. To get a particular element, write the name of the list and then the index of the element in square brackets.
- List can store both Homogeneous and heterogeneous elements.
- Syntax of Accessing list element of list :
>>> list_name[index]>>> users[0] # It gives you First element of list. i.e. "shruti"
>>> users[1] # It gives you Second element of list. i.e. "vishal"
>>> users[-1] # It gives you last element of list.
Length of list :
>>> sample_list = [1,2,3,4,5]
>>> len(sample_list) # len(collection) : Syntax
Concatenation of Two list :
>>> sample_list = [1,2,3,4,5]
>>> sample_list1 = [6,7,8,9,10]
>>> sample_list = sample_list + sample_list1
# '+' --> used to concatenation
List Iteration:
>>> users = ["shruti", "Ronny", "Razz", "Ram", "hari"]
# 1st way by index
>>>for index in range(0,len(users)):
print(users[index])# 2nd way by 'in' keyword
>>>for user in users:
print(user)
Modifying the elements in list :
Once you’ve defined a list, you can change individual elements in the list. You do this by referring to the index of the item you want to modify.
>>> users[2] = “razz” # It will modify 3rd element of list.
>>> users[-1] = “rahul” # It will modify last element of list.
List Slicing :
if requirement is sublist from a list with limited no of element.
Syntax :
list[start : end :jump]
- start → starting index(by default = 0)
- end → ending index (by default = len(list)), it is exclusive.
- jump/step (by default jump = 1)
e.g :
we have list of 6 elements :
>>>list3 = [1,2,3,4,5,6]
- if you want element from index 1 to index 3 as sub-list, so we need to apply slicing here:
>>>list3[1:4] # start = 1 , end = 3+1 , jump = 1
- if you want element from start to end but jump would be 2 as sub-list
- Reverse the list without using reverse function:
List built-in function :
1. append(element) : Add element to end of list.
2. insert(position,element) :Insert an element at specific position.
3. index(element) : Return the First occurrence index of element. if not found then throw valueError.
4. pop(index) : Remove & returns an element at given index position.
5. remove(element) : Remove first occurrence of element.
6. sort() : Sort list in ascending order.
7. reverse() : It will reverse the list.
Thank you…