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

Python List

A Python list is a general purpose container that can hold elements of any type. Lists are constructed using brackets [] and commas to separate elements. Elements in a list can be accessed by their index number, and lists support operations like slicing, adding/removing elements, sorting, and more using built-in functions like len(), append(), pop(), sort(), and others. Lists are mutable and can change in size dynamically as elements are added or removed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views

Python List

A Python list is a general purpose container that can hold elements of any type. Lists are constructed using brackets [] and commas to separate elements. Elements in a list can be accessed by their index number, and lists support operations like slicing, adding/removing elements, sorting, and more using built-in functions like len(), append(), pop(), sort(), and others. Lists are mutable and can change in size dynamically as elements are added or removed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

List in Python

What is List?
 A Python list is a general purpose 1dimensional
container for variables.

 Variables in a list can be of any type at any


location, including other lists.

 Lists can change in size: elements can be added or


removed

 Python List values are accessible using its


reference and index
Making a List
 Lists are constructed with brackets [] and commas
separating every element in the list.

listName = [value1, value2]

 Example Program

my_list = [‘String’, 100, 2.2, 10>9]


List Indexing
 grab element from list through index. The index of first
value/element is always 0.

my_list = [‘String’, 100, 2.2, 10>9]

my_list[0] my_list[3]
List Slicing
 We can slice list with the used of colon [:]

 my_list[:1]

 my_list[1:]
List Functions
 len() – use to count list items
◦ len(my_list)

 append() - merely appends an element to the end of


a list: appends whole object at end:
◦ my_list.append([4,5])

 count() - takes in an element and returns the number


of times it occurs in your list
◦ my_list.count(4)

 extend - extends list by appending elements from


the iterable:
◦ my_list.extend([4,5])
List Functions
 index() - will return the index of whatever element is placed
as an argument. Note: If the element is not in the list an error
is raised.
◦ my_list(value)

 insert() - takes in two arguments:


insert(index,object). This method places the object at the
index supplied
◦ my_list.inset(2, ‘insert’)

 pop() - which allows us to "pop" off the last element of a list.


However, by passing an index position you can remove and
return a specific element.
◦ my_list.pop(index/default_last)
List Functions
 remove() - removes the first occurrence of a value
◦ my_list.remove(element/value_to_be_removed)

 reverse() - reverses a list. Note this occurs in place!Meaning


it affects your list permanently.
◦ my_list.reverse()

 sort() - method will sort your list in place:


◦ my_list.sort()
◦ my_list.sort(reverse=True)

 copy()- use to make an exact copy of list


◦ variable = my_list.copy()

You might also like