top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Python Lists

Let us make a list of Family members like father, mother, brother, sister.

In python we write as

Family=['father','mother','brother','sister','baby']

How about listing birthday dates of our family members,22,13,2,11,25

Birthdates=[22,13,2,11,25]

Both family and Birthdates are the examples are python lists. Individual Things inside a list is called items.

Example: Family=['father','mother','brother','sister','baby']


How do we Define List?

Python list is used to store multiple items in a single variable. A list is a data structure in Python that is a mutable, or changeable ordered sequence of elements. Lists are used to store multiple data at once.


What can List Hold?

Lists can hold any kind of data that Python can store. It includes numbers, strings, objects, and even other lists. The items in a list don’t have to be the same type or kind of thing. That means a single list can hold both numbers and strings.

Example:

As I am making a list of family members and birth dates,

Mylist=['father',22,'mother',13,'brother',2,'sister',11,'baby',25]


List Operations


1. Getting individual items or data from list

Each item in a list is identified by its index. Any single item in a list can be extracted using its index number. The list index starts from 0, which means first item will have index or position as zero.

Example,

Family

Father

Mother

Brother

Sister

index

0

1

2

3

In above list with variable Family,

Family[0] = 'Father' and Family [1] = 'Mother'



2. Negative Index

Index -1 represents the last element and -n represents the first element of the list (considering n as the length of the list). Lists can also be manipulated using negative indexes also.

Example: If we want to get the Last Item from Family List

3. Slicing a List

Getting more than one item from a list at a time is called Slicing a list.

The slicing operator “:” can be specified to with start and end positions to achieve the slicing.


The number of items we get back is always the difference between the two index numbers (3-0=3). It returned 3 items. When you slice a list, we obtain a smaller list. The smaller list is called the slice of the original list. The original list remains changed.



How to slice without start index?

If no starting index is specified, it will be defaulted to zero. Ending index specified will be considered.



There is no number before the colon. This will give you everything from start of the list up to (but not including) The index you specify.

By passing 2 in the square brackets, slicing starts from the 0 index (by default, since start not specified) up until the second index we specified.

As mentioned earlier, the slicing excludes the value at the second index. So, in the results, as you find in the copy variable, the values returned are from index 0 through index 1.


How to slice without end index ?


If no ending index is specified, it will be defaulted to the end of the list. Starting index specified will be considered.


There is no number after the colon. This will give you everything from start position specified up to end of the list.

By passing 2 in the square brackets, slicing starts from the 2 index and go up to end of the list (by default, since end not specified).


How to slice without start and end index

If no start and end positions are specified, it takes zero as start and last item's postion as the end.



How to slice with steps ?

Slice[start:stop:step]

  • start specifies the starting position of the slice.

  • stop species the ending position of the slice.

  • step specifies the increment to be considered while selecting the values from the list.

Example,

Mylist=['father',22,'mother',13,'brother',2,'sister',11,'baby',25]

I want to slice this Mylist list into 2 slices. 1 slice hold’s family member list.

Another list holds the date of birth of family members.



How to slice with negative start and end indexes ?

The start or stop indexes can also be negative. Negative indexes count from the end of the list which means negative index is the last value in a list.

The slice starts from index -4(which is the fourth value from the end of the list, that is ‘Mother’) and stops at index -1 (which is the last value in the list, that is ‘baby’). Slicing doesn't include the last value, so we have 'mother’,’brother’,’sister’.


How to slice with negative steps ?

Negative steps can be used, which means the steps decrement for the slicing. Example below,


Here, we specify a start of index 4 hence slicing start from ‘baby’, and end of index as 1 a step of -2. Slicing here will start from index 4 which is ‘Baby’. The negative steps mean the next value in the slice will be at an index smaller than the previous index by 2. That is 4-2 is 2. It will print index 2 as “brother”. Hence the result of sliced array is “Baby” and “Brother”.


4. Reversing a List

  • A list can be reversed just by using negative step,

example Below returns the reversed list. The start of the index by default is 0. And step size is -1 (0-1 = -1), that is “baby”. -1-1 is -2 which is “sister”, -2-1 is -3 which is “brother”,-3-1 is -4 which is “mother”,-4-1 is -5 which is “father”. That is like a reverse of a list.




  • A list can be reversed using reverse() as well. It helps reversing the order of the list.


5. Modify Items in a List

The items in a list can be modified or updated by using the position of the index. Below example depicts the original and updated list post update using index position.


Note: List items can be updated/modified as above but a new item cannot be added using index.


6. Adding Items into a List

New items can be added into the list either through Append(), Insert() or ExtendI() functions.

INSERT()

Insert adds an item to the list at the specified position.

In the below example, Grand Father is added to the zeroth index/position.

APPEND()

Append adds an item to the list at the end. If length of the index is not known, append becomes very handy.

In the below example, "Grand Mom" is being added at the end of the list.

EXTEND()

Extend adds single or multiple items to the list at the end. This too is very handy if length of the list is unknown and multiple values to be added.

In the below example, "Grand Father" and "Grand Mother" are being added at the end of the list.


The key difference between Extend and Append is that, Extend can add multiple values to the list whereas Append expects single value.


7. Delete Items from a List

Items can be dropped or deleted from a list using removeI(), del() or Pop() functions.

Remove()

Remove function helps to delete the specified item from the list.

In the below example of Family list, remove function is helping to drop "sister" from the list.

Del() Del function helps to delete the item based on the index specified.


In the below example, index 3 is specified to delete, and hence sister is being removed.


Pop()

Pop removes the last item from the list unless index is specified. If index is specified, particular item in the index will be removed. It also returns the item removed.


In the below example, last indexed item "baby" is being removed since no index was specified. It displayed the last family member that it removed.


In below example it popped out the index 1 item from the Family list that is ‘Mother’. It also displayed the item which got removed from the list.


8. Sorting Items in a List

Item in a list can be ordered in ascending or descending order using sort and sorted functions.


Sort()

Sort function sorts the string alphabetically and numbers numerically from smallest to largest. It sorts and changes the original copy of the list as well.


In the below example, Family list is sorted in the ascending order. Also, the original list is changed with ascending order.


In the below example, Family list is being sorted in descending order due to the "reverse=True" parameter passed with sort. Similarly, original list gets changed here.

sorted()

Sorted function sorts copy of list without changing the order of the original List.


In the below example, the list is sorted and original list remain unchanged


9. Finding the Index

Index function helps finding the index of the item specified.


In the below example, index of the mother is being fetched.


10. Searching List

In() function is used to confirm if the specified item is part of the list or not.


In the below example, father is being searched in the list, and output confirm the existence.


In the below example, "Grand Mom" is being searched in the list, and output shows that doesn't exist in the list.



There are various other functions to parse the list, and few critical ones are explained in this blog.

21 views1 comment

Recent Posts

See All