List

1. DATA STRUCTURES

Data structures are basically just that – they are structures which can hold some data together. In other words, they are used to store a collection of related data. There are four built-in data structures in Python – list, tuple, dictionary and set.

2. LIST IN PYTHON

A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list. A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

2.1 CREATE A LIST IN PYTHON

For example:

thislist = ["apple", "banana", "cherry"]
print(thislist)

OUTPUT:

CREATE LIST IN PYTHON
OUTPUT – CREATE LIST IN PYTHON

2.2 ACCESS ITEMS IN LIST IN PYTHON

You can access the list items by referring to the index number.

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

OUTPUT:

ACCESS LIST ITEM IN PYTHON
OUTPUT – ACCESS LIST ITEM IN PYTHON

2.3 CHANGE ITEM VALUE IN LIST IN PYTHON

To change the value of a specific item, refer to the index number.

thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)

OUTPUT:

CHANGE ITEM IN LIST IN PYTHON
OUTPUT – CHANGE ITEM IN LIST IN PYTHON

2.4 ITERATE (loop) THROUGH A LIST IN PYTHON

You can loop through the list items by using for loop.

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

OUTPUT:

ITERATE ITEMS IN LIST IN PYTHON
OUTPUT – ITERATE ITEMS IN LIST IN PYTHON

2.5 CHECK IF ITEM EXISTS IN LIST IN PYTHON

To determine if a specified item is present in a list use the in keyword:

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

OUTPUT:

CHECK ITEM IN LIST IN PYTHON
OUTPUT – CHECK ITEM IN LIST IN PYTHON

2.6 LIST LENGTH IN PYTHON

To determine how many items a list has, use the len() method:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

OUTPUT:

LIST LENGTH IN PYTHON
OUTPUT – LIST LENGTH IN PYTHONa

2.7 ADD ITEMS IN LIST IN PYTHON

2.7.1 TO ADD AN ITEM TO THE END OF THE LIST, USING append() METHOD:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

OUTPUT:

ADD ITEM/ APPEND IN LIST PYTHON
OUTPUT – ADD ITEM/ APPEND IN LIST PYTHON

2.7.2 TO ADD AN ITEM AT THE SPECIFIED INDEX, USE THE insert() METHOD IN LIST IN PYTHON

thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

OUTPUT:

INSERT ITEM IN LIST IN PYTHON
OUTPUT – INSERT ITEM IN LIST IN PYTHON

2.8 REMOVE ITEM FROM LIST IN PYTHON

There are several methods to remove items from a list. They are:

2.8.1 Remove – FROM LIST IN PYTHON

The remove() method removes the specified item.

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

OUTPUT:

output remove item in list in python
OUTPUT – REMOVE ITEM IN LIST IN PYTHON

2.8.2 Pop – FROM LIST IN PYTHON

The pop() removes the specified index, ( or the last item if index is not specified):

thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

OUTPUT:

POP ITEM OF LIST IN PYTHON
OUTPUT – POP ITEM of LIST IN PYTHON

2.8.3 del – FROM LIST IN PYTHON

The del keyword removes the specified index.

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

OUTPUT:

DELETE SPECIFIC INDEX ITEM FROM LIST IN PYTHON
OUTPUT – DELETE SPECIFIC INDEX ITEM FROM LIST IN PYTHON

2.8.4 del – FROM LIST IN PYTHON

The del keyword can also delete the list completely:

thislist = ["apple""banana""cherry"]
del thislist
print(thislist)

OUTPUT:

DELETE LIST IN PYTHON
OUTPUT – DELETE LIST IN PYTHON

2.8.5 clear – FROM LIST IN PYTHON

The clear() method empties the list.

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

OUTPUT:

DELETE LIST IN PYTHON
OUTPUT – DELETE LIST IN PYTHON

Leave a Reply

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