LISTS & TUPLES
LISTS
• an ordered set of data values.
• values that make up a list elements or items.
• logical structure similar to that of a string.
• Each item unique index that specifies its position
• items are ordered by their positions
CREATING LISTS
• enclose the elements in a pair of square brackets
• Elements separated by coma
• Examples:
• List1=[2,3,5,7,9]
• Flowers=[‘rose’,’jasmine’,’lily’]
• Fruits=[‘apple’,’orange’,’mango’]
• List2=[1,2,[1,2,3]] here [1,2,3] member list
• List3=[1,2,’Anju’, [1,9]]
• List2 & List3 are nested liststhey have another list as an element
• [] null list
CREATE LISTS USING range( ) & list ( )
FUNCTIONS
• list1=list(range(10)) list1=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
• list2=list(range(1,10)) list2=[1, 2, 3, 4, 5, 6, 7, 8, 9]
• list3=list(range(1,14,3)) list3=[1, 4, 7, 10, 13]
FINDING LIST OF A STRING len()
FUNCTION
program
List1=[1,3,5,7]
Print(len(List1))
Output
4
ACCESSING ELEMENTS OF A LIST
Students=['Gowri','Grace','Grushab','Hamda
n']
for x in Students:
print(x)
LIST COMPREHENSION
Students=['Gowri','Grace','Grushab','Hamdan']
Gstudents=[x for x in Students if x[0]=='G']
print(Gstudents)
+ CONCATANATION
* REPEATS A LIST A GIVEN NUMBER OF TIMES
==CHECKS IF TWO LISTS HAVE THE SAME
ELEMENTS.
RELATIONAL OPERATORS
MEMBERSHIP
OPERATOR
USING
VARIABLES AS
ELEMENTS IN
LIST
LIST SLICES
LIST MUTATIONS
• Lists are mutatable
• Strings are not
• Elements can be inserted, removed, or
replaced.
REPLACE AN ELEMENT
INSERT, DELETE
DELETE
STRINGS & LISTS
LIST ALIASING AND CLONING
ALIASING-SAME LIST CLONING-COPY OF
DIFFERENT NAME ORIGINAL
IDENTITY OPERATOR TO CHECK ALIAS
AND CLONE
TUPLE
• Similar to list but immutable.
• enclosing its elements in
parenthesis
• elements separated by
commas
•
• with a single elementinclude a
comma at the end, even though
there is only one value.
TUPLE CREATION-OTHER METHODS