Introduction to python List data type:

List data type is the most widely used data type in python. So first of all let’s understand, What is meant by List in python? List is the data type format in python, Which is used to store data in list format i.e in an ordered sequence of elements.

Members of list are called as list items or elements. This list is mutable(changeable) i.e You can edit the list over the course of time.

So let’s begin to learn the list data type.

How to create a List in python:

In python list can be created by placing the list items in square bracket [] and each list item is separated by comma.

List items may of any data type (Integer, string, float etc.). Also there is no limit on number of list items. Below are the few examples of python lists.

numbers_list = [1, 2, 3, 4, 5]
strings_list = ["Jack", "Lily", "John"]
mixed_data_list = [1, 2.5, "Paul", 0.5]

Also in python you can create Nested lists as well. That means one list behaves as an list item/element for the another list.

nested_list = ["Hello", 2.6, [1, 2, 3, 4, 5], "Apple", 5]

Accessing the list items/elements:

To perform any operation with the python list, We need to have access of each elements. So the way to access each element of the list is to index them.

List Indexing:

List indexing means, For each list element we assign an unique number (Index number). Using this index number it is very easy to access any element of the list. We just need to specify the index number in specific format.

So in python list indexing starts with the zero “0” index number. That means first list element always has zero “0” index number. Suppose you are having a list with 6 list elements then indexing will start from 0 to 5. Now to access the list element indexing operator [] “Square bracket” is used. By specifying an index number in square bracket we get the control over that specific list element.

List data type and indexing in python

List data type indexing in python

Furthermore it is also possible do list indexing from last list element as well. This is called as negative list indexing. As a result of this negative list indexing, Last list element will be assigned with -1 index number and second last element will get -2 index number and so on.

Examples:

Let’s see few examples of how we can access and print the list element.

List data type indexing and printing in python

Examples of list elements indexing

  • In first example above we have created a list of string elements. To print the single list element we need specify in following format;
    • print (name_of _list [Index_number_of_list_element])
      print(list_of_strings[2])
  • List created in second example is the list of different data types. To print multiple list elements we have to specify range of index numbers in following format;
    • print (name_of _list[index number range separated by colon])
      print(list_of_mixed_data[2:5])
  • List created in Third example is nested list. One list is present within another list. We can access the list within just by specifying its index number.
    • print (name_of _list[index number range separated by colon])
      print(nested_list[1:3])

How to Add, Edit and Remove list elements:

Since the list data type in python is mutable. Therefore it is possible to add and remove the list elements.

Now let’s see each operation one by one.

1. Replacing the list element in python:

Assignment operator ( = ) is used to Change/Replace the existing list element.

Suppose we are having existing list (number_list = [1, 3, 4, 5]). Now we want to replace the number “1” from list by number “2”. In the code below we have specified the index number and new value.

number_list = [1, 3, 4, 5]
number_list[0] = 2  #specify the index number of element to be changed and its new value.
print(number_list)

# Result
[2, 3, 4, 5]

2. Adding a single or multiple list elements in python:

For adding new single element we have to use append() method. Further if we want to add multiple elements we have to use extend() method.

number_list = [1, 3, 4, 5]
number_list.append(6)      #Specify the list_name.append along with element value.
print(number_list)

# Result
[1, 3, 4, 5, 6, 7, 8]

number_list.extend([7, 8]) #Specify the list_name.extend along with list of elements we want to add.
print(number_list)

# Result
[1, 3, 4, 5, 6, 7, 8]

3. Adding list element at desired location:

If we use append or extend method to add new element by default the elements gets added at the end of list. But if we want to add new element at specific location in the list then, We have to use insert() method.

Old_list = [2, 4, 6]
Old_list.insert(1, 'hello') #Insert word 'Hello' at index number 1 location.
print(Old_list)

# Result
[2, 'hello', 4, 6]

4. List concatenation in python:

Furthermore in python it is possible to join multiple lists. Using concatenation operator “+” it is possible to join multiple lists.

first_list = [2, 4, 6]
second_list = [8, 10, 12]
print(first_list + second_list) #List concatenation using "+" operator.

# Result
[2, 4, 6, 8, 10, 12]

5. Deleting list element in python:

  • There are actually two ways of removing the list elements.
    • Specify the index number of element that we want to delete. Use del() method
    • Specify the element value to be removed from the list. Use remove() method.
first_list = [2, 4, 6]
del(first_list[1]) #Delete the element by specifying its index number.
print(first_list)

# Result
[2, 6]

Example of removing element by its value.

second_list = ['Jack', 'Lily', 'John']
second_list.remove('Lily') #Remove an element by specifying element value.
print(second_list)

#Result
['Jack', 'John']

There are few more python methods available to work with the list data type. Below is the list of methods which you can try out.

Methods in pythonResults obtained
append()Add a new element at the end of the list.
extend()Add multiple elements at the end of list.
insert()Insert new element at desired index location.
remove()Removes an element from the list
pop()Removes and returns an element at the given index
clear()Removes all elements from the list.
index()Finds and returns the index of the first matched element.
count()Returns the count of number of elements passed as an argument
sort()Sort list elements in a ascending order
reverse()Reverse the order of list elements.
copy()Shallow copy the list

 

Continue Reading…

Also you can get more help on python forum.  Click Here

Share this Post

Leave a Comment

Your email address will not be published.