01 Lists in Python Class
01 Lists in Python Class
Lists in Python
❖ Introduction to Lists
❖ Creating a List
❖ list() function
❖ Nested Lists The CS Pundit
Lists in Python
1. Introduction to Lists
2. Creating and Accessing List Elements
3. Understanding List Mutability
4. List Operations: Concatenation, Repetition, Membership, Slicing
5. Traversing a List
6. List Methods: append, clear, copy, count, extend, index, insert, pop, remove, reverse, sort
7. Built-In Functions for Lists: list(), len(), sorted(), min(), max(), sum(), any(), all(), enumerate()
8. Working with Nested Lists
9. Copying and Comparing Lists
10. Using Lists as Function Arguments
11. List Manipulation and Programming: This includes both manipulating lists and creating
list-based programs.
12. And More : This is not an exhaustive list, and there are many more aspects of Python lists to explore
and understand.
Introduction to Lists in Python
Python lists are a built-in data type that can store a sequence of items. Each item, or element, in a list can be of any data
type, such as integers, floats, strings, tuples, or even other lists. This versatility makes lists a powerful tool for grouping
together a mix of data types.
1. Order: Lists maintain the order of elements based on their insertion order.
2. Mutability: Unlike strings and tuples, lists can change their elements in place without creating a new list.
3. Duplication: Lists can contain duplicate values.
4. Zero-Based Indexing: List indices start from 0, like string indices.
5. Heterogeneity: Lists can store objects of different types.
6. Dynamism: Lists can grow or shrink dynamically, supporting the addition, insertion, and removal of elements.
7. Nesting: Lists can contain other lists, allowing for complex data structures like lists of lists.
8. Traversal: Lists support iteration, allowing traversal using a loop to perform operations on each element.
In Python, Lists, Sets, and Dictionaries are known as collection data types, while Lists, Tuples, and Strings are sequence
data types. Notably, lists are unique as they fall into both categories. They are often referred to as containers or collections
because they can hold a variable number of objects. These objects are enclosed in square brackets `[]` and separated by
commas `,`, making lists both a collection and a sequence, and thus particularly versatile.
Creating a List
Creating an Empty List
An empty list is a list that contains no elements. This is useful when you need a list, but you don't have any
elements to put in it yet.
empty_list = []
The simplest way to create a list is to place the items (elements) inside square brackets [], separated by
commas.
b= ['NCERT', 'CBSE']
print(b) #Output : ['NCERT', 'CBSE']
Creating a List with Different Types of Items
A list in Python can contain items of different types. For example, a list can contain a mix of integers,
strings, floats, and booleans.
>>>['Class', 11]
list() function in python
We can create a Python list by using list() function. Below are the ways by which we can use list() function
in Python:
s='List in Python'
char_list=list(s)
char_list
['L', 'i', 's', 't', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n']
list() function in python
Creating a List from a Tuple
The `list()` function can also be used to create a list from a tuple.
>>>t=('computer science','information practices')
>>>list(t)
['computer science', 'information practices']
You can create a list from a range using the range() function combined with the list() function.
>>>list(range(11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>list(range(10,13))
[10, 11, 12]
2D list in python
>>>list2D = ['python', ['class', 11], ['class',12]]
>>>list2D
['python', ['class', 11], ['class', 12]]
3D list in python
>>>list3D=['python',['class 11',['cs','chapter 9'],['ip','chapter 4 ']]]
>>>list3D
['python', ['class 11', ['cs', 'chapter 9'], ['ip', 'chapter 4 ']]]
Join Telegram Channel For Free PDFs & PPTs
Telegram Channel : https://t.me/TheCSPundit
Youtube Channel : @TheCSPundit