1.lists and List Slices
1.lists and List Slices
Data Structures
● Many real-world problems are easier to solve when simple data types are
combined into more complex data structures.
● Modeling the data that your program interacts with using the right data
structure is often the key to writing simple and effective code.
● Python has three built-in data structures: tuples, lists, and dictionaries.
Lists
Creating Lists
● A list is surrounded with square brackets [ ]
Lists
● Lists values can be any data type.
1. A list literal
● You can create a new list from an existing once using slice notation:
● You can check for the existence of list elements using the in operator:
The concatenation (+) and replication (*) operators:
Del command
A list item can be deleted with the del command:
● Because lists are iterable, you can iterate over them with a for loop.
Changing Elements in a List
The ability to swap values in a list for other values is called mutability. Lists are
mutable.
● The values in these slots are assigned to "orange" and "magenta", respectively.
The list assigned to a slice does not need to have the same length as the slice.
For instance, you can assign a list of three elements to a slice with two elements:
When the length of the list being assigned to the slice is less than the length of the
slice, the overall length of the original list is reduced: