[go: up one dir, main page]

0% found this document useful (0 votes)
8 views4 pages

List

The document explains how to store, access, modify, and slice data in Python lists, highlighting their versatility and mutability. It also discusses list aliasing, demonstrating how multiple variables can reference the same list and how to create independent copies. Additionally, it covers working with nested lists and accessing characters in strings using indexing.

Uploaded by

MyDream 4U
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)
8 views4 pages

List

The document explains how to store, access, modify, and slice data in Python lists, highlighting their versatility and mutability. It also discusses list aliasing, demonstrating how multiple variables can reference the same list and how to create independent copies. Additionally, it covers working with nested lists and accessing characters in strings using indexing.

Uploaded by

MyDream 4U
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/ 4

STORING AND ACCESSING DATA IN LISTS

In Python, lists are versatile data structures used to store collections of


items. These items can be of various data types, including numbers, strings, and
even other lists.

STORING DATA IN LISTS


Lists are created using square brackets [], with items separated by
commas.
my_list = [1, "hello", 3.14, True]

ACCESSING DATA IN LISTS


Elements within a list are accessed using their index, starting from 0 for the
first element.
print(my_list[0]) # Output: 1
print(my_list[1]) # Output: hello

Negative indexing can also be used to access elements from the end of the
list, with -1 representing the last element.
print(my_list[-1]) # Output: True
print(my_list[-2]) # Output: 3.14

MODIFYING DATA IN LISTS


Lists are mutable, meaning their elements can be changed after creation.
my_list[0] = 10
print(my_list) # Output: [10, "hello", 3.14, True]

SLICING LISTS
Subsets of lists can be extracted using slicing.
my_list=[10, "hello", 3.14, True]
new_list = my_list[1:3]
print(new_list) # Output: ['hello', 3.14]

ALIASING LIST IN PYTHON PROGRAM


List aliasing in Python occurs when two or more variables refer to the same
list object in memory. This means that changes made to the list through one
variable will be reflected when accessing the list through another variable.

Since variables refer to objects, if we assign one variable to another, both


variables refer to the same object:

list1 = [1, 2, 3]
list2 = list1 # list2 now refers to the same list as list1

print(list1) # Output: [1, 2, 3]


print(list2) # Output: [1, 2, 3]

list2.append(4) # Modify the list through list2

print(list1) # Output: [1, 2, 3, 4] - list1 is also changed


print(list2) # Output: [1, 2, 3, 4]

In the example above, list1 and list2 are aliases because they both point to the
same list in memory. When list2 is modified, the change is also observed in list1.
This behavior can be useful in some situations, but it can also lead to
unexpected results if not handled carefully.
To avoid aliasing and create an independent copy of a list, you can use the copy()
method or slicing:

list1 = [1, 2, 3]
list2 = list1.copy() # Create a new list with the same elements
# or
list3 = list1[:] #slicing also creates a new list

list2.append(4)
print(list1) # Output: [1, 2, 3] - list1 is unchanged
print(list2) # Output: [1, 2, 3, 4]

list3.append(5)
print(list1) # Output: [1, 2, 3] - list1 is unchanged
print(list3) # Output: [1, 2, 3, 5]
In this case, list2 and list3 are independent copies of list1, so modifying one will
not affect the others.

WORKING WITH A LIST OF LISTS.


A list of lists, also known as a nested list, is a list where each element is itself a
list. These are useful for representing two-dimensional data like matrices or
tables.
Creating a list of lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

PROCESSING CHARACTERS IN STRINGS


Accessing Characters
Individual characters in a string can be accessed using their index, starting from
0 for the first character. Negative indices can be used to access characters from
the end of the string, with -1 being the last character.
my_string = "Hello"
for char in my_string:
print(char)
# Output:
#H
#e
#l
#l
#o
my_string = "Hello World"
print(my_string[0:5]) # Output: Hello
print(my_string[6:]) # Output: World

You might also like