What Is List In Python

What Is List In Python

A list is a type of data structure in Python that is a mutable, or changeable, ordered sequence of elements.Each element or value that is inside of a list is termed as item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].

  • In contrast with tuples, the length of lists is variable and their contents can be modified. They can be defined using square brackets [ ].

  • Here, a list is defined using comma separated values of mixed data types.

  • We can view the content of the list by just referring to the list object.

  • You can use the append method to add a value to the list. Note that this value gets added to the end of the list.

  • We can also remove any particular item by just referring to the element value.

  • Use the pop method to simultaneously view and remove the value at a particular index.

  • Similarly, use the insert method to insert a value at a particular index.

List In Python


How To Print the elements of List In Python

CL = [100, 5.2, 'class', True]
print (CL)

Output:

[100, 5.2, 'class', True]

How To Update The Values In List In Python

To update the list elements you have to access first the element of the list by it's index value

List1 = ['physics', "chemistry", 1997, 2000]
print (List1)
print ()     

print ('Original Value at Index 2 = ', List1[2])          # access the value at index 2
List1[2] = "maths" # update 1997 to maths
print ()        

print ('Updated Value at Index 2 = ', List1[2])          # access the value at index 2
print ()
print ('Updated List = ', List1)

Output:

['physics', 'chemistry', 1997, 2000]

Original Value at Index 2 =  1997

Updated Value at Index 2 =  maths

Updated List =  ['physics', 'chemistry', 'maths', 2000]

How To Delete An Element Of List In Python 

List1 = ['physics', "chemistry", 1997, 2000]
print (List1)
print ()                                      #========================== Blank print

print ('Original Value at Index 2 = ', List1[2])          # access the value at index 2

del List1[2]

print ()
print ('Updated Value at Index 2 = ', List1[2]) # access the value at index 2

print ()
print ('Updated List = ', List1)

Output:

['physics', 'chemistry', 1997, 2000]

Original Value at Index 2 =  1997

Updated Value at Index 2 =  2000

Updated List =  ['physics', 'chemistry', 2000]

How To Access Elements From List In Python

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1,2,3,4,5,6,7]

print (list1)
print ()
print (list2)


Output:

['physics', 'chemistry', 1997, 2000]

[1, 2, 3, 4, 5, 6, 7]

print (list1[0])
print (list2[1:5]) # [Start : Stop - 1]

Output:

physics

[2, 3, 4, 5]


How To Concatenate element values in List In Python


aList = [123, 'xyz', 'zara', 'abc']
bList = [2009, 'mani']

print (aList)
print (bList)
print ()
print (aList + bList)
print (bList + aList)


Output:

[123, 'xyz', 'zara', 'abc']
[2009, 'mani']

[123, 'xyz', 'zara', 'abc', 2009, 'mani']
[2009, 'mani', 123, 'xyz', 'zara', 'abc']

How To Do The Repetition Of The Elements In List In Python


aList = [123, 'xyz', 'zara', 'abc']

print (aList * 2)

Output:

[123, 'xyz', 'zara', 'abc', 123, 'xyz', 'zara', 'abc']

Different Inbuild Functions Of List In Python

# list append() -- Modify - adding a new value - appends an element to the end of the list


aList = [123, 'xyz', 'zara', 'abc']
print ('Original List : ', aList)
print ()

aList.append(2009)

print ('Updated List : ', aList)

Output:

Original List :  [123, 'xyz', 'zara', 'abc']

Updated List :  [123, 'xyz', 'zara', 'abc', 2009]

# Modify a list: Insert a new item at a certain index


aList = [123, 'xyz', 'zara', 'abc']
print ('Original List : ', aList)
print ()

aList.insert(2, 2009) # index and value --- insert 2009 at index 2
print ('Updated List : ', aList)

Output:

Original List :  [123, 'xyz', 'zara', 'abc']

Updated List :  [123, 'xyz', 2009, 'zara', 'abc']

# Remove - element -- Modifying the list
# remove deletes the item, does not display anything


aList = [123, 'xyz', 'zara', 'abc', 'xyz']
print ('List = ', aList)
print ()

aList.remove("xyz")
print ('List = ', aList)
print ()

aList.remove('zara')
print ('List = ', aList)


Output:

List =  [123, 'xyz', 'zara', 'abc', 'xyz']

List =  [123, 'zara', 'abc', 'xyz']

List =  [123, 'abc', 'xyz']

# pop() - The pop() method removes the item at the given index from the list and returns the removed item.

aList = [123, 'xyz', 'zara', 'abc']
print ('List = ', aList)
print ()

print (aList.pop(2)) # print the element or value which will be removed
print ()
print ('List = ', aList)
print ()

print (aList.pop()) # print the element or value which will be removed. when no indice is mentioned picks the last one
print ()
print ('List = ', aList)

Output:


List =  [123, 'xyz', 'zara', 'abc']

zara

List =  [123, 'xyz', 'abc']

abc

List =  [123, 'xyz']

#########How To Clear The List Elements

aList = [123, 'xyz', 'zara', 'abc']
print (aList)
print ()
# remove (aList)

aList.clear()  
print (aList) # empty list

del aList # now the list names aList doesn't exist
print (aList) # NameError: name 'aList' is not defined

Output:

[123, 'xyz', 'zara', 'abc']

[]

Error

Comments