0% found this document useful (0 votes)
15 views5 pages

List

Lists in Python are a built-in data type used to store ordered, changeable collections of items, which can include duplicates and various data types. They are created using square brackets and can be manipulated with various methods such as append, extend, sort, and remove. Lists are defined as objects of the data type 'list' and can be constructed using the list() constructor.

Uploaded by

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

List

Lists in Python are a built-in data type used to store ordered, changeable collections of items, which can include duplicates and various data types. They are created using square brackets and can be manipulated with various methods such as append, extend, sort, and remove. Lists are defined as objects of the data type 'list' and can be constructed using the list() constructor.

Uploaded by

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

List

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities
and usage.

Lists are created using square brackets:

ExampleGet your own Python Server


Create a List:

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


print(thislist)
Try it Yourself »

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
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:

Example
Lists allow duplicate values:

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


print(thislist)
Try it Yourself »

ADVERTISEMENT

List Length
To determine how many items a list has, use the len() function:

Example
Print the number of items in the list:

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


print(len(thislist))
Try it Yourself »

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]
Try it Yourself »

A list can contain different data types:

Example
A list with strings, integers and boolean values:

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


Try it Yourself »

type()
From Python's perspective, lists are defined as objects with the data type
'list':

<class 'list'>
Example
What is the data type of a list?

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


print(type(mylist))
Try it Yourself »

The list() Constructor


It is also possible to use the list() constructor when creating a new list.

Example
Using the list() constructor to make a List:

thislist = list(("apple", "banana", "cherry")) # note the double


round-brackets
print(thislist)
Try it Yourself »
➢A list is an ordered sequence of values.
➢It is a data structure in Python. The values inside the lists can be of any type (like integer,
float, strings, lists, tuples, dictionaries etc) and are called as elements or items.
➢Lists are constructed using square brackets [ ] wherein you can include a list of items
separated by commas.
➢For example,


ls1=[10,-4, 25, 13]
ls2=[“Tiger”, “Lion”, “Cheetah”]
List Operations / List Methods
There are several built-in methods in list class for various purposes. Here, we will discuss some
of them.
1. append(): ➢This method is used to add a new element at the end of a list.
➢Example: ls=[1,2,3] ls.append(„hi‟) ls.append(10) print(ls)

output: [1, 2, 3, „hi‟, 10]


2. extend(): ➢This method takes a list as an argument and all the elements in this list are added
at the end of invoking list.
➢Example: ls1=[1,2,3] ls2=[5,6] ls2.extend(ls1) print(ls2)

output: [5, 6, 1, 2, 3]
3. sort(): ➢This method is used to sort the contents of the list. By default, the function will sort
the items in ascending order.
➢Example : ls=[3,10,5, 16,-2]

ls.sort() print(ls)
output:[-2, 3, 5, 10, 16]
4. reverse(): ➢This method can be used to reverse the given list.
➢Example: ls=[4,3,1,6] ls.reverse()
5. count(): ➢This method is used to count number of occurrences of a particular value within
list.
➢Example ls=[1,2,5,2,1,3,2,10]

print(ls)
output: [6, 1, 3, 4]
ls.count(2)
output : 3 #the item 2 has appeared 3 tiles in ls
6. clear(): ➢This method removes all the elements in the list and makes the list empty.
➢Example ls=[1,2,3] ls.clear() print(ls) output: [ ]
➢Used to insert a value before a specified index of the list.
➢Example: ls=[3,5,10] ls.insert(1,"hi") print(ls)

7.insert():
output: [3, 'hi', 5, 10]
8. index(): ➢This method is used to get the index position of a particular value in the list.
➢Example: ls=[4, 2, 10, 5, 3, 2, 6]

ls.index(10) output: 3
9. pop(): ➢This method deletes the last element in the list, by default.
➢Example: ls=[3,6,-2,8,10]

x=ls.pop() #10 is removed from list and stored in x print(ls) output: [3, 6, -2, 8]
print(x) output :10
10. remove(): ➢When we don‟t know the index, but know the value to be removed, then this
function can be used.
➢Example: ls=[5,8, -12,34,2]

ls.remove(34) print(ls)
output: [5, 8, -12, 2]

You might also like