What is list in python
Lists are just like dynamically sized arrays, declared in other languages
(vector in C++ and ArrayList in Java).
The elements in a list are indexed
according to a definite sequence and the indexing of a list is done with 0
being the first index. Each element in the list has its definite place in the
list, which allows duplicating of elements in the list, with each element
having its own distinct place and credibility.
---------------------
Basic Properties
of a List:
- Ordered: The items in a list have a specific order, and
that order is maintained.
- Mutable: Lists can be modified, meaning you can add,
remove, or change items in the list after it has been created.
- Allows Duplicates: Lists can have duplicate
elements (i.e., the same value can appear multiple times).
- Heterogeneous: Lists can hold elements of different types
(e.g., integers, strings, objects).
Syntax for
Creating a List:
We create a list by placing elements inside square brackets ([]), separated by commas.
List_Name=[element1,element2,.....] my_list = [1, 2, 3, 4, 5]
Basic
Operations on Lists:
- Creating a List:
We can create a list containing different types of elements.
my_list = [1, "apple", 3.14, True] print(my_list)
Output:
[1, 'apple', 3.15, True]
- Accessing List Items:
Lists are indexed, starting from 0. You can access an element by using its index.
my_list = [1, "apple", 3.14, True] print(my_list[1]) # Accessing the element at index 1 (second element)
Output:
apple
- Negative Indexing:
Negative indices allow us to access list elements starting from the end. -1 refers to the last item, -2 refers to the second last item, and so on.
print(my_list[-1]) # Accessing the last element
Output:
True
- Slicing a List:
We can extract a part of a list (a sublist) using slicing. The syntax is my_list[start:end], where start is the index where the slice starts, and end is the index where it ends (but excluding the element at the end index).
my_list = [1, 2, 3, 4, 5] print(my_list[1:4]) # Slice from index 1 to index 3 (not including index 4)
Output:
[2, 3, 4]
- Changing List Elements:
Since lists are mutable, we can change their values by assigning a new value to an index.
my_list = [1, 2, 3, 4, 5] my_list[0] = 10 # Changing the first element print(my_list)
Output:
[10, 2, 3, 4, 5]
- Adding Elements to a List:
There are
different ways to add elements to a list:
- Using append(): Adds an element to the end
of the list.
- Using insert(): Inserts an element at a
specific position in the list.
- Using extend(): Adds multiple elements to the end of the list.
my_list = [1, 2, 3] # Using append() my_list.append(4) # Using insert() (insert 99 at index 1) my_list.insert(1, 99) # Using extend() (adding multiple elements) my_list.extend([5, 6]) print(my_list)
Output:
[1, 99, 2, 3, 4, 5, 6]
Example: Enter five elements in list then print those elements along with sum value.
n=[]; for i in range(5): num=int(input("enter elements")); n.append(num); print(n); print(sum(n));
Output:
enter elements5
enter elements6
enter elements3
enter elements9
enter elements3
[5, 6, 3, 9, 3]
26
Syn: list.insert(index, element)
l = [8, 'Harry', 9.3] print(l) ele=input("Enter Element") ind=int(input("Enter Index no.")) l.insert(ind, ele) print(l)
- Removing Elements from a List:
We can
remove elements from a list in several ways:
- Using remove(): Removes the first occurrence
of a specified element.
- Using pop(): Removes and returns an
element at a specified index (if no index is provided, it removes the
last element).
- Using del: Removes an element at a
specified index.
- Using clear(): Removes all elements from
the list.
Example:
my_list = [1, 2, 3, 4, 5]
# Using remove() (removes first occurrence of 3)
my_list.remove(3)
Example: Enter five list elements then remove all elements.
n=[]; for i in range(5): num=int(input("Enter elements")); n.append(num); print(n); ch=input("Do you want remove all elements(y/n):"); if ch=='y': n.clear(); print(n);
Output:
Enter elements5
Enter elements6
Enter elements9
Enter elements3
Enter elements7
[5, 6, 9, 3, 7]
Do you want remove all elements(y/n):y
[]
# Using pop() (removes element at index 2, returns the
value)
popped_element = my_list.pop(2)
# Using del (removes element at index 0)
del my_list[0]
# Using clear() (removes all elements)
my_list.clear()
print(my_list) #
After clearing the list
Output:
css
[]
- List Length:
You can find the number of elements in a list using the len() function.
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
Output:
5
- List Membership:
We can check if an element exists in a list using the in keyword.
my_list = [1, 2, 3, 4, 5]
print(3 in my_list)
# Returns True if 3 is in the list
print(6 in my_list)
# Returns False if 6 is not in the list
Output:
True
False
- List Comprehension:
A concise way to create lists using a single line of code. List comprehension is often more efficient and readable.
squares = [x**2 for x in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
List
Methods:
Here are some commonly used methods
associated with Python lists:
- append(x): Adds item x to the end of the list.
- insert(i, x): Inserts item x at position i.
- remove(x): Removes the first occurrence of item x.
- pop([i]): Removes and returns item at position i. If no index is provided, removes and returns
the last item.
- clear(): Removes all items from the list.
- index(x): Returns the index of the first occurrence of
item x.
- count(x): Returns the number of times item x appears in the list.
- sort(): Sorts the list in ascending order.
- reverse(): Reverses the order of the list.
List = []
print("Blank List: ")
print(List)
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
# Creating a List of strings and accessing
# using index
List = ["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])
Summary:
- A list is an ordered, mutable collection
of items in Python.
- Lists can contain items of different types.
- You can access, modify, and manipulate lists
using various methods like append(), insert(), remove(), pop(), and others.
- Lists support indexing, slicing, and can be
iterated over with loops.
0 Comments