list in python

A list in python is a sequence of values. values can be any type. The values in the list are called elements or items.

What is the list in Python? | A Definition

list is a data structure in Python. A list in Python is used to store the sequence of various types of data. Lists store multiple items in a single variable.  lists are defined by having values between square brackets [ ].

How to define a list in python?

How to create a list in Python? There are several ways to create a new list in Python. The simplest way to enclose the items or elements in square brackets. for example:

list = [“item1”, “item2”, “item3”]

The second method is using the list() Constructor. for example,

list = list( (“element1”, “element2”, “element3”) )

Note: enclose all the elements in round brackets and pass it as one argument otherwise you will get the following error.

TypeError: list expected at most 1 arguments

A list can be of same type or it can contain different data types. Here are some examples for list in python.


# Examples
#list in python

#list of four integers
int_list = [10, 20, 30, 40]

#list of fruits
fruits = ["apple", "banana", "pineapple"]

#list (mixed data types)
myData = ["male", 11, True, 40, "USA"]

## Empty list
list = []

How lists are mutable in python?

lists are mutable because you can change the order of items in a list or reassign an item in a list.

The following example explains, how lists are mutable in python.



    numbers    =    [10,    20]

    # change the second element
    numbers[1]    =    5

# output: [10,    5]


In the above example the second element is now 5 instead of 20.

list as a relationship between indices and elements. This relationship is called ” mapping “. Each index “maps to” one of the elements in a list.

Here are some rules related to indices and elements.

  • The index should be an integer or an integer expression can be used as an index.
  • Reading or writing an element that does not exist in the list results in an IndexError.
  • If an index has a negative value, it counts backward from the end of the list.

How to traverse the list in python?

The most common way to traverse the elements of a list is with a for loop. The syntax of for loop and traversing is given below.

## Example

for item  in list: 


This will display the elements of the list.

If you want to write or update the elements, you need the indices.

For example in the following example we will update the list.

## Example

for i   in  range(len(list)): 
    list[i] =  list[i] * 2


This loop traverses the list and updates each element. len() returns the length of the number of elements in the list. range() returns a list of indices from 0 to n − 1, where n is the length of the list.

Each time through the loop, i gets the index of the next element.

The assignment statement ( = ) in the body uses i to read the old value of the element and to assign the new value.

How to slice lists in Python?

slicing is a flexible tool to build new lists out of an existing list. The slice operator looks like this:

## Example
## slice in python

list[start:end]  # items start through end-1

list[start:]      # items start through the rest of the array

list[: end]       # items from the beginning through end-1

list[:]           # a copy of the whole array


Here is a simple example of slicing in Python

## Example

   t  =  ['a',    'e',    'i',    'o',    'u']
   # output: ['e', 'o'] 

   # output: ['a',    'e',    'i',    'o'] 

   #output: ['o',    'u']


You can also specify steps as a third value.

Note : end value is not included in output. the output will be end-1

## Example

# list[start:end:step]

    numbers    =    [10, 20, 10, 20, 30 , 3, 4, 7, 8, 8, 7, 9, 0, 7]

#this code will print every 2nd element from index 1 to 8
    #output: [20, 20, 3, 7]


What are the list methods in Python?

Python provides some built-in methods to manipulate the lists in python. for example, to add a new element to a list we use append() method. Here are examples for each method for list in python.

append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add lists and combine all the elements of specified lists into a single list.
index()Returns the index of the specified element of the list
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the first item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

#Append Method
list = ['a', 'b', 'c', 'd']
#output: ['a', 'b', 'c', 'd', 'e']

#Clear Method
list = ['a', 'b', 'c', 'd']
#output: []

#Copy Method
list = ['a', 'b', 'c', 'd']
list2 = list.copy()
#output: ['a', 'b', 'c', 'd']

#Count Method
list = ['a', 'b', 'b', 'c', 'd']
print( list.count('b') )
#output: 2

#Extend Method
list = ['a', 'b', 'c', 'd']
list2 = ['e', 'f', 'g', 'h']
print( list.extend(list2) )
#output: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

#Index Method
list = ['a', 'b', 'b', 'c', 'd']
print( list.index('b') )
#output: 2

#Insert Method
list = ['a', 'b', 'c', 'd']
list.insert(1, "e")
#output: ['a', 'e', 'b', 'c', 'd']

#Pop Method
list = ['a', 'b', 'c', 'd']
#output: ['a', 'c', 'd']

#Remove Method
list = ['a', 'b', 'c', 'd']
#output: ['b', 'c', 'd']

#Reverse Method
list = ['a', 'b', 'c', 'd']
#output: ['d', 'c', 'b', 'a']

#Sort Method
list = ['b', 'a', 'c', 'v', 'd']
#output: ['a', 'b', 'c', 'd', 'v']


Youtube video to learn list in python


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *