• In Python, a list is a collection data type that is
ordered, mutable (can be changed), and allows duplicate elements. Lists are one of the most used data structures in Python and are defined using square brackets ([]). • Features of Lists: • Ordered: Elements have a defined order, which List will not change unless explicitly modified. • Mutable: You can modify elements (add, remove, change). • Allows Duplicates: Lists can contain multiple elements with the same value. • Heterogeneous: Lists can store items of different data types (e.g., integers, strings, other lists). Creating list Access the elements in a list The eval() function • The eval() function is used to evaluate and return the result of an expression as string Updating values in list • Once created, one or more elements of a list can be easily updated by giving the slice on the left hand side of the assignment operator. You can also append new values in the list and remove existing value(s) from the list using the append() method and del statement, respectively. Updating values in lists • If you know exactly which element(s) to delete, use the del statement otherwise use the remove() method to delete the unknown elements. Updating values in list Relational operations in lists Nested lists List aliasing and cloning Deleting elements Deep copies and Shallow copies in list Basic list operations List methods Tuple A tuple is very similar to lists but differs in two things. ➢ First, a tuple is an immutable object. This means that while you can change one or more elements in a list, you cannot change values in a tuple. ➢Second, tuple use parentheses to define it’s elements, whereas list uses square brackets Creating a Tuple Key points to remember Key points to remember…. Key points to remember….. Utility of tuples Accessing values in a tuple • You can’t delete elements from a tuple. Methods like remove or pop do not work with tuple Dictionaries • Dictionary is a data structure in which we store values as a pair of key and value. Each key is separate from it’s value by a colon(:), and consecutive items are separated by commas. The entire set of items in a dictionary is enclosed in curly braces({}). The syntax of defining a dictionary is: • Dictionary name= {key_1:value_1, key_2: value_2, key_3:value_3} Creating Dictionaries Creating dictionary using dict() method Creating Dictionary using keys and values that are stored in tuples Accessing values in a dictionary Modifying an item in dictionary Deleting items The pop() method Keys should not be duplicated in a dictionary
Keys should be strictly of a type that is immutable
The keys() method: To list all keys used in the dictionary, in an arbitrary order. Use the sorted function to sort the keys in keyword Traversing a dictionary