Essential things to know about List data structure in python

Essential things to know about List data structure in python

DATE : 22/08/2019

INTRODUCTION

In python, the list is defined as a collection of items separated by a comma within the square bracket.

which is best to use when we want to work with different values.

LIST

  • one of the main thing about a list is that items in a list need not be of the same type.
  • which is used to store multiple data at the same time.
  • Lists are mutable, they can be altered even after their creation.

LIST OPERATIONS:

Access the list:

list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008]

#print the list in python

print(list);

#access the list items using index

print "print the first value: ", list[0]

#access the items using slice operator

print "list[5:12]: ", list[5:12]

RESULT:

APPEND THE LIST

#append operations

list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008]

#append will add the in value end of the list
list.append("mango")

print(list);

#append the one list with another list
list1 = ["apple", "orange", "banana"]

#list1 append with the list
list.append(list1)

print(list)

RESULT:

LIST COUNT

#return the number of times the value "physics" appears int the list
list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008,"physics"]

x = list.count("physics")

print(x)

RESULT

EXTEND THE LIST

list1 = ["apple", "orange", "banana"]
list2 = ["mango"]


list1.extend(list2)

print(list1)

RESULT

INSERT VALUE TO LIST

#insert the value to particular position
list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008,"physics"]

list.insert(1, "english")

print(list)

POP THE LIST

#pop(delete) the list
list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008,"physics"]


list.pop(1)

print(list)

RESULT

REMOVE

#remove the value
list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008,"physics"]

list.remove(23)

print(list)

RESULT

REVERSE

#reverse the list 
list = ["Math",0,1,2,4,23,56,32,234,437,987,"physics","chemistry",1997,2008,"physics"]

list.reverse()

print(list)

RESULT

SORT

#sort the list
list = [1997,0,1,2,4,2018,23,234,56,32,437,987,2008]
list.sort()

print(list)

RESULT

Thanks for using pheonix solutions.

You find this tutorial helpful? Share with your friends to keep it alive.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top