0% found this document useful (0 votes)
6 views

PYTHON LISTS

The document outlines four collection data types in Python: List, Tuple, Set, and Dictionary, detailing their characteristics. It focuses on Lists, describing their creation, properties (ordered, changeable, allows duplicates), and various methods (e.g., append, clear, extend, insert, pop, remove, reverse, sort) for manipulating list items. Examples are provided to illustrate the usage of lists and their methods.

Uploaded by

DineshGarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PYTHON LISTS

The document outlines four collection data types in Python: List, Tuple, Set, and Dictionary, detailing their characteristics. It focuses on Lists, describing their creation, properties (ordered, changeable, allows duplicates), and various methods (e.g., append, clear, extend, insert, pop, remove, reverse, sort) for manipulating list items. Examples are provided to illustrate the usage of lists and their methods.

Uploaded by

DineshGarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Collections (Arrays)

There are four collection data types in the Python programming language:

• List is a collection which is ordered and changeable. Allows duplicate


members.
• Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
• Set is a collection which is unordered, unchangeable, and unindexed. No
duplicate members.
• Dictionary is a collection which is ordered and changeable. No duplicate
members.

PYTHON LISTS

➢ Lists: In Python, a list is created by placing elements inside square brackets [],
separated by commas. A list can have any number of items and they may be of
different types (integer, float, string, etc.).
Example: Create a List:
a = ["apple", "banana", "cherry"]

List Items: List items are ordered, changeable, and allow duplicate values.List
items are indexed, the first item has index [0], the second item has index [1] etc.

• Ordered: When we say that lists are ordered, it means that the items have
a defined order, and that order will not change.If you add new items to a
list, the new items will be placed at the end of the list.

Note: There are some list methods that will change the order, but in
general: the order of the items will not change.

• Changeable (Mutable): The list is changeable, meaning that we can


change, add, and remove items in a list after it has been created.
• Allow Duplicates: Since lists are indexed, lists can have items with the
same value.
List Items - Data Types

List items can be of any data type:

Example
String, int and boolean data types:

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


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

A list can contain different data types:

Example
A list with strings, integers and boolean values:

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

Methods:
1. Len(): To determine how many items a list has, use the len() function:
Example: list1 = ["apple", "banana", "cherry"]
print(len(list1))
o/p= 3

2. append(): The append() method appends an element to the end of the list.

Syntax: list.append(elmnt)

Parameter Description

elmnt Required. An element of any type (string, number, object etc.)

Example:
Add a list to a list:

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


b = ["Ford", "BMW", "Volvo"]
a.append(b)

3. clear():
Syntax: list.clear()

Example:
fruits = ['apple', 'banana', 'cherry', 'orange']

fruits.clear()

4. extend():
Syntax: list.extend(iterable)

Parameter Description

iterable Required. Any iterable (list, set, tuple, etc.)

Example

Add a tuple to the fruits list:


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

p = (1, 4, 5, 9)

fruits.extend(p)

5. insert():
Syntax: list.insert(pos, elmnt)

Parameter Description

pos Required. A number specifying in which position to insert the value

elmnt Required. An element of any type (string, number, object etc.)


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

fruits.insert(1, "orange")

6. pop():
Syntax: list.( pos)

Parameter Description

pos Optional. A number specifying the position of the element you want to
remove, default value is -1, which returns the last item

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

a= fruits.pop(1)

7. remove():
Syntax: list.(elmnt)

Parameter Description

elmnt Required. Any type (string, number, list etc.) The element you
want to remove

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

fruits.remove("banana")

8. reverse():
Syntax: list.reverse()
Example:
fruits=['apple', 'banana', 'cherry']

fruits.reverse()

9. sort():The sort() method sorts the list ascending by default.


Syntax: list.sort(reverse=True|False, key=myFunc)

Parameter Description

reverse Optional. reverse=True will sort the list descending. Default is


reverse=False

key Optional. A function to specify the sorting criteria(s)

You might also like