Python offers a range of compound data types called sequences and List is one of the most frequently used and most versatile datatypes used in Python. List contains items, and items here are known as "elements".
Create a List
A list is created by placing all the elements inside the brackets with double quotes separated by commas. It can have any number of elements and can have integers (numbers), floats (fractional numbers), strings (characters), etc., or a combination of these in a single list. It can also have another list as an element. Suppose, we want to create a list containing weekdays starting Monday to Thursday. It is as simple as assigning a name in Visual Basic. Just create an array in a single line- should be specified inside the brackets with double quotes separated by comma as shown below-
workingdaysList = ["Monday", "Tuesday", "Wednesday", "Thursday"]
print (workingdaysList)
Append Elements to a List
Appending an element or a list of elements to an existing list- is one of the most common activities performed by any programmer. The easiest way to append elements is by using the 'append()' command. Suppose, we want to add/append one element (say "Friday") to our existing list- which can be done by typing the existing list name to which we want to append an element followed by the 'append()' command. The element which we want to add should be specified inside the parentheses with double quotes as shown below-
workingdaysList.append("Friday")
print (workingdaysList)
What if we want to append a list instead of a single element?
Suppose, we have another list with elements as shown below-
weekendList = ["SUNDAY", "SUNDAY"]
print (weekendList)
The above list ("weekendList") does not have the exact required elements (i.e., "Saturday" and "Sunday") but we can use the other elements which are contained in this list (i.e., "Sunday" or other elements if the list goes long). To append the same, we can mention the list name which we want to append inside the parentheses as shown below-
workingdaysList.append(weekendList)
print (workingdaysList)
Instead of using the 'append()' command used above, and if we have some other element (say "Saturday") to include in the list along with appending the second list to the first list, we can simply create a new list (named "weekdaysList") which contain elements from both the existing lists ("workingdaysList" & "weekendList") and then we can also include some more elements as per our requirement (in our case, we want to include "SATURDAY"). Basically, we want to concatenate/combine the lists and elements, and this can easily be done with the "+" sign as shown below-
weekdaysList = workingdaysList + weekendList + ["SATURDAY"]
print (weekdaysList)
Remove Elements from a List
Of course, we can remove elements from the list. In our example, we want to remove one "SUNDAY" from the list ("weekdaysList") by using the 'remove()' command as shown below-
weekdaysList.remove("SUNDAY")
print (weekdaysList)
One can also use the 'pop()' command in Python to remove elements from the list. But note that in this command, a programmer has to specify the index number instead of the element name as shown below-
weekdaysList.pop(5)
print (weekdaysList)
Update/Modify Elements in a List
Updating elements in the list can be done with the help of the Index Number. Python always indexes the elements available in the list and assigns a specific number to each element. The index number starts with 0. In our case, 0 is assigned to Monday, 1 is assigned to Tuesday, 2 is assigned to Wednesday, and likewise to all the elements in the list. In short, a list having 7 elements will have an index from 0 to 6.
Whenever a programmer wants to refer to any specific element from the list, he has to specify the index number in the parentheses.
In our example, we want to update two elements in the list (i.e., "SUNDAY" and "SATURDAY") from uppercase to replace them with "Sunday" & "Saturday" lowercase. And the index number are 5 & 6 respectively. To refer to any element in the list, simply type the list name followed by the index number in the brackets. To assign or update the item, use the "=" sign and specify the updated name as shown below-
weekdaysList[5] = "Sunday"
weekdaysList[6] = "Saturday"
print (weekdaysList)
Python also allows negative index referencing for its indexed items. The index of -1 refers to the last element, -2 to the second last element, and so on.
Interchange/Rearrange Elements in a List
Suppose, if we want to interchange certain elements in the list- in our example, we want to interchange "Sunday" and "Saturday" with "Saturday" and "Sunday". This can be done by typing the list name followed by the index number in the brackets and then it can be equals to the item to which you want to interchange with.
weekdaysList[5],weekdaysList[6] = weekdaysList[6],weekdaysList[5]
print (weekdaysList)
Extract Few/Specific Elements from a List
By referring to the index number, one can extract elements from the list. Such reference can be done by specifying the list name followed by the index number in the brackets as shown below-
print(weekdaysList[5], weekdaysList[6])
Reverse the Order of the Elements in a List
It is very simple to reverse the order of the elements by simply giving the 'reverse()' command to the existing list followed by the open and close parentheses as shown below-
weekdaysList.reverse()
print (weekdaysList)
Sort the Elements in a List
It is very simple to sort the elements by simply giving the 'sort()' command to the existing list followed by the open and close parentheses as shown below-
weekdaysList.sort()
print (weekdaysList)
The list is one of the most frequently used and very versatile data types used in Python. All the above commands are the most widely used in almost every industry which are using Python for data analysis.
Comentários