Lists in Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

LISTS IN PYTHON AND FUNCTIONS

A list is an ordered and mutable Python container, being one of the most common
data structures in Python. To create a list, the elements are placed inside square
brackets ([]), separated by commas. Lists can contain elements of different types as
well as duplicated elements. List is a compound data type which means you can have
different-2 data types under a list, for example we can have integer, float and string
items in a same list.

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

Example
Lists allow duplicate values:

thislist = ["apple", "banana", "cherry", "apple", "cherry"]


print(thislist)

Example
String, int and boolean data types:

list1 = ["apple", "banana", "cherry"]


list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

Example
A list with strings, integers and boolean values:

list1 = ["abc", 34, True, 40, "male"]

We can perform following functions with list, in Python -

1. Append function: The append() method appends an element to the end


of the list.

Syntax : list.append(element)

Example
Add a list to a list:

a = ["apple", "banana", "cherry"]


b = ["Ford", "BMW", "Volvo"]
a.append(b)
Example
Adding elements to an empty list:

list=[] # Creating an empty list


print(list) # Printing an empty list
list.append(1) # Inserting a number - 1, in the list, using append() function
list.append('abc') # Inserting a word - abc, in the list, using append() function
list.append(102) # Inserting another number - 102, in the list, using append() function
print(list) # Printing the list with elements

2. INSERT FUNCTION - The Python insert() method adds item to a list at a


specific position in a list. insert() accepts two arguments. These are the position
of the new item and the value of the new item you want to add to your list. Like
the append() method, insert() adds an item to an existing list.

Example
Insert the value "orange" as the second element of the fruit list:

fruits = ['apple', 'banana', 'cherry']

fruits.insert(1, "orange")

3. EXTENDED FUNCTION -The extend() method adds the specified list


elements (or any iterable) to the end of the current list.

Example
Add the elements of cars to the fruits list:

fruits = ['apple', 'banana', 'cherry']

cars = ['Ford', 'BMW', 'Volvo']

fruits.extend(cars)

Example
Add a tuple to the fruits list:

fruits = ['apple', 'banana', 'cherry']

points = (1, 4, 5, 9)

fruits.extend(points)
Removing Elements from a List

Elements from a list can removed using two methods :

1) Using remove() method

The remove() method removes the first occurrence of the element with the
specified value.

Example
Remove the "banana" element of the fruit list:

fruits = ['apple', 'banana', 'cherry']

fruits.remove("banana")

2) Using pop() method

The pop() method removes the element at the specified position.

Example
Remove the second element of the fruit list:

fruits = ['apple', 'banana', 'cherry']

fruits.pop(1)

You might also like