Python List Built
Python List Built
Python provides the following built-in functions, which can be used with
the lists.
1. len()
2. max()
3. min()
len( )
It is used to calculate the length of the list.
Code
Output:
Max( )
It returns the maximum element of the list
Code
Output:
782
Min( )
It returns the minimum element of the list
Code
Output:
103
Code
1. list1 = [1,2,2,3,55,98,65,65,13,29]
2. # Declare an empty list that will store unique values
3. list2 = []
4. for i in list1:
5. if i not in list2:
6. list2.append(i)
7. print(list2)
Output:
Code
1. list1 = [3,4,5,9,10,12,24]
2. sum = 0
3. for i in list1:
4. sum = sum+i
5. print("The sum is:",sum)
Output:
Code
1. list1 = [1,2,3,4,5,6]
2. list2 = [7,8,9,2,10]
3. for x in list1:
4. for y in list2:
5. if x == y:
6. print("The common element is:",x)
Output:
The main difference between the two is that we cannot alter the
components of a tuple once they have been assigned. On the other hand,
we can edit the contents of a list.
Example
Forming a Tuple:
All the objects-also known as "elements"-must be separated by a comma,
enclosed in parenthesis (). Although parentheses are not required, they
are recommended.
Any number of items, including those with various data types (dictionary,
string, float, list, etc.), can be contained in a tuple.
Code
Output:
Empty tuple: ()
Tuple with integers: (4, 6, 8, 10, 12, 14)
Tuple with different data types: (4, 'Python', 9.3)
A nested tuple: ('Python', {4: 5, 6: 2, 8: 2}, (5, 3, 5, 6))
Code
Output:
Code
1. # Python program to show how to create a tuple having a single element
2. single_tuple = ("Tuple")
3. print( type(single_tuple) )
4. # Creating a tuple that has only one element
5. single_tuple = ("Tuple",)
6. print( type(single_tuple) )
7. # Creating tuple without parentheses
8. single_tuple = "Tuple",
9. print( type(single_tuple) )
Output:
<class 'str'>
<class 'tuple'>
<class 'tuple'>
Indexing
The indices of a tuple with five items will range from 0 to 4. An Index Error
will be raised assuming we attempt to get to a list from the Tuple that is
outside the scope of the tuple record. An index above four will be out of
range in this scenario.
Code
12. try:
13. print(tuple_[1.0])
14. except Exception as e:
15. print(e)
16. # Creating a nested tuple
17. nested_tuple = ("Tuple", [4, 6, 2, 6], (6, 2, 6, 7))
18.
19. # Accessing the index of a nested tuple
20. print(nested_tuple[0][3])
21. print(nested_tuple[1][1])
Output:
Python
Tuple
tuple index out of range
tuple indices must be integers or slices, not float
l
6
o Negative Indexing
The last thing of the assortment is addressed by - 1, the second last thing
by - 2, etc.
Code
2. # Creating a tuple
3. tuple_ = ("Python", "Tuple", "Ordered", "Collection")
4. # Printing elements using negative indices
5. print("Element at -1 index: ", tuple_[-1])
6. print("Elements between -4 and -1 are: ", tuple_[-4:-1])
Output:
Slicing
Tuple slicing is a common practice in Python and the most common way
for programmers to deal with practical issues. Look at a tuple in Python.
Slice a tuple to access a variety of its elements. Using the colon as a
straightforward slicing operator (:) is one strategy.
To gain access to various tuple elements, we can use the slicing operator
colon (:).
Code
Output:
Deleting a Tuple
A tuple's parts can't be modified, as was recently said. We are unable to
eliminate or remove tuple components as a result.
Code
1. # Python program to show how to delete elements of a Python tuple
2. # Creating a tuple
3. tuple_ = ("Python", "Tuple", "Ordered", "Immutable", "Collection", "Object
s")
4. # Deleting a particular element of the tuple
5. try:
6. del tuple_[3]
7. print(tuple_)
8. except Exception as e:
9. print(e)
10. # Deleting the variable from the global space of the program
11. del tuple_
12. # Trying accessing the tuple after deleting it
13. try:
14. print(tuple_)
15. except Exception as e:
16. print(e)
Output:
Output:
Tuple Methods
Like the list, Python Tuples is a collection of immutable objects. There are
a few ways to work with tuples in Python. With some examples, this essay
will go over these two approaches in detail.
o Count () Method
Code
1. # Creating tuples
2. T1 = (0, 1, 5, 6, 7, 2, 2, 4, 2, 3, 2, 3, 1, 3, 2)
3. T2 = ('python', 'java', 'python', 'Tpoint', 'python', 'java')
4. # counting the appearance of 3
5. res = T1.count(2)
6. print('Count of 2 in T1 is:', res)
7. # counting the appearance of java
8. res = T2.count('java')
9. print('Count of Java in T2 is:', res)
Output:
Count of 2 in T1 is: 5
Count of java in T2 is: 2
Index() Method:
The Index() function returns the first instance of the requested element
from the Tuple.
Parameters:
Code
1. # Creating tuples
2. Tuple_data = (0, 1, 2, 3, 2, 3, 1, 3, 2)
3. # getting the index of 3
4. res = Tuple_data.index(3)
5. print('First occurrence of 1 is', res)
6. # getting the index of 3 after 4th
7. # index
8. res = Tuple_data.index(3, 4)
9. print('First occurrence of 1 after 4th index is:', res)
Output:
First occurrence of 1 is 2
First occurrence of 1 after 4th index is: 6
Code
Output:
True
False
False
True
Code
1. # Python program to show how to iterate over tuple elements
2. # Creating a tuple
3. tuple_ = ("Python", "Tuple", "Ordered", "Immutable")
4. # Iterating over tuple elements using a for loop
5. for item in tuple_:
6. print(item)
Output:
Python
Tuple
Ordered
Immutable
Changing a Tuple
Tuples, instead of records, are permanent articles.
This suggests that once the elements of a tuple have been defined, we
cannot change them. However, the nested elements can be altered if the
element itself is a changeable data type like a list.
Code
Output:
'tuple' object does not support item assignment
('Python', 'Tuple', 'Ordered', 'Immutable', [1, 2, 10, 4])
('Python', 'Items')
The + operator can be used to combine multiple tuples into one. This
phenomenon is known as concatenation.
Code
Output: