You are on page 1of 5

LISTS

In programming, it is common to work with collections of data. In Python, a list is


one of the many built-in data structures that allow us to work with a collection of
data in sequential order.

1-Dimension List Important Notes:

1. A list begins and ends with square brackets [ and ].


2. Each item in a list is separated by a comma
Example = [56 , 12, 21, 51, 23]
3. We can even combine multiple data types in one list.
4. An example of a mixed list is [“mia”,34,6.02, True,” Ahmed”, False]
5. An empty list can be created by [ ]
Example : an_empty_list = [ ]
6. To add something to a list we use the popular method,
list_name.append (data_to_be_added_to_list)
if data is a string we use “ ” inside the append’s parenthesis.
7. To remove something from a list we use the method called remove.
list_name.remove (data_to_be_removed)
if data is a string we use “ ” inside the remove’s parenthesis.
8. Another method to add something to a list is adding a list to the existing list
or adding the item manually.
new_list = old_list + random_list
new_list = old_list + [ “Apple”, 50 , “Mangoes , 20 ]
9. For accessing items from a list from an index, (index is the value of positions
in a list, and in python, indexing starts from 0.)
10. Index number will always be an integer and it cannot be float or string
otherwise it will generate the error.
11. The syntax to print or take an item from a list is:
print (list_name[0]) # it will print the first item in the list.
Similarly, we can use negative indexing as well, where [-1] will represent the
last item in the list.
12. To modify the list at a certain index can be done by using the syntax:
list_name[index_no.] = “item to be added” or integer.
Note that it will permanently remove the existing item and modify it for the
new one.

2D-Dimension Lists Important Notes:

1. A 2D list looks like fruits = [[“Apples”,20],[“Banana,25],[“Cherry”,12]]


2. Accessing a 2D list requires double indexing, one index indicating the
primary list and the other for the items in that list.
for example if in the above example we want to access the price of a
banana:
price_of_banana = fruits[1][1]
3. For modifying a 2D list the method is the same but again double indexing
will be used, for example, to change the price of cherry to 20 we use:
fruits[2][-1] = 20
WORKING WITH LIST ON PYTHON:

Important Python List Methods:

1. .count ():
A list method to count the number of occurrences of an element in a list.
For example, we want to find how many times a word or integer appeared
in a list can be found by: using list_name.count(“item_name” or number)

2. .insert ( ):
A list method to insert an element into a specific index of a list.
Comment: (Previously, we studied that to add something to a list we use amend.() or
addition method but that method will only add the item at the end of the list. If we want
to add an item at a certain index we need to use .insert().
Syntax for .insert( ) is:

list_name.insert( index_number, value_to_be_added)

3. .pop():
A list method to remove an element from a specific index or from the end
of a list.
Comment: (Previously, we used .remove() to remove an item from the list but that
requires the item name or value, what if we want to remove the item using its index. So
we use .pop() to remove an item existing at that index. If the space between parenthesis
is left empty the last item [-1] index, will get removed automatically.

syntax for .pop() is:


list_name.pop(2) #remove item at index 2. All items will shift to the left.
4. Range ():
A built-in Python function to create a sequence of integers.
Example: If we want to add a sequence of integers to a list for example we want a list to
have integers from 0 to 9.
There are two ways to do this:

list_name = range (0:10) # 10 will not be included, starting number can be changed.
list_name = range (21) # so if we want to print 0 to 20. We put range 21.
To print these numbers we will use print but with another syntax list.

print(list(list_name))
we can also print sequences using the range, for that we use the following syntax:

list_name = range( x, y , z )
where x = initial value (it will be included in the range),
y = ending value (it will not be included in the range but all numbers before will)
z = common difference between x and y.

5. len() :
A built-in Python function to get the length of a list, we can find the length
of any list and store or print it using the syntax:
length_of_list = len(list_name)
print(len(list_name)

6. Sort ( )/ Sorted()
A method and built-in function to sort a list. There are two methods
sorted_list = list_name.sort() # you can use reverse=True in ( ) for backwards
sorted_list = sorted(list_name)

SLICING A LIST:
In Python, often we want to extract only a portion of a list. Dividing a list in such a
manner is referred to as slicing.
The syntax for slicing a list is:

List_name [starting_index: ending_index]


Starting_index = index from which the slicing starts and that item will be included in the list
ending_index = index before which the slicing will end and the item at that index won’t be
added to the sliced list.

COMBINING TWO LISTS:

Syntax :
Combine_list = zip(list_1,list_2)
Print(list(Combine_list))

You might also like