0% found this document useful (0 votes)
9 views

1.lists and List Slices

list

Uploaded by

vani shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

1.lists and List Slices

list

Uploaded by

vani shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lists

Data Structures
● Many real-world problems are easier to solve when simple data types are
combined into more complex data structures.

● A data structure models a collection of data, such as a list of numbers, a


row in a spreadsheet, or a record in a database.

● 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

The important characteristics of Python lists are as follows:

● Lists are ordered.


● Lists can contain any arbitrary objects.
● List elements can be accessed by index.
● Lists can be nested to arbitrary depth.
● Lists are mutable.
● Lists are dynamic.
Lists Are Mutable Sequences
● Lists contain items that are indexed by integers,starting with 0.
● Lists are mutable, meaning you can change the value at an index even after
the list has been created.

Creating Lists
● A list is surrounded with square brackets [ ]
Lists
● Lists values can be any data type.

● The list literal ["one", 2, 3.0] is perfectly valid.


● You can also use the list() built-in to create a new list object from any other
sequence.
● For instance, the tuple (1, 2, 3) can be passed to list() to create the list [1, 2,
3]:
You can even create a list from a string:

Each letter in the string becomes an element of the list.


You can create a list from a string of a comma-separated list of items using the
string object’s .split() method:

The string argument passed to .split() is called the separator.


● By changing the separator you can split strings into lists in numerous ways:
Three ways to create a list:

1. A list literal

2. The list() built-in

3. The string .split() method


List Elements Can Be Accessed by Index
Individual elements in a list can be accessed using an index in square brackets.

List indexing is zero-based

The indices for the elements in a are shown below:


A negative list index counts from the end of the list:
Slicing
If a is a list, the expression a[m:n] returns the portion of a from index m to, but not
including, index n:

Both positive and negative indices can be specified:


Slicing
Omitting the first index starts the slice at the beginning of the list, and omitting the
second index extends the slice to the end of the list:
Stride
You can specify a stride—either positive or negative:
The syntax for reversing a list works the same way it does for strings:

If a is a list, a[:] returns a new object that is a copy of a:


Basic List Operations
● Indexing and slicing operations work on lists.
● You can access list elements using index notation:

● 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 value at index 0 changes from "red" to "burgundy":


● You can change several values in a list at once with a slice assignment:

● colors[1:3] selects the slots with indices 1 and 2.

● 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:

You might also like