CSE 1001 (Python) Faculty Name: Dr. AMIT Kumar Tyagi
CSE 1001 (Python) Faculty Name: Dr. AMIT Kumar Tyagi
List Creation
Lists are created using a comma separated list of values surrounded by
square brackets.
Lists hold a sequence of values (just like strings can hold a sequence of
characters).
Lists are very easy to create, these are some of the ways to make lists
emptyList = [ ]
numlist = [1, 3, 5, 7, 9]
List Length
With the length function we can get the length of a list
len(list)
>>4
List Append
List append will add the item at the end.
If you want to add at the beginning, you can use the insert function (see
below)
list.insert(0, "Files")
list.append("Files")
print (list)
List Insert
The syntax is: list.insert(x, y) #will add element y on the place before x
list.insert(2,"Documents")
print list
list.insert(3, "Apps")
List Remove
To remove an element's first occurrence in a list, simply use list.remove
list.remove("Files")
print (list)
a = [1, 2, 3, 4]
a.remove(2)
print a
[1, 3, 4]
List Extend
The syntax is: list.extend(x) #will join the list with list x
list1.extend(list2)
print list1
NOTE: A list is an object. If you append another list onto a list, the second list will be
a single object at the end of the list.
another_list = [6, 0, 4, 1]
my_list.append(another_list)
print my_list
another_list = [6, 0, 4, 1]
my_list.extend(another_list)
print my_list
['geeks', 'for', 6, 0, 4, 1]
NOTE: A string is an iterable, so if you extend a list with a string, you’ll append
each character as you iterate over the string.
my_list.extend('geeks')
print my_list
List Delete
Use del to remove item based on index position.
del list[1]
print list
if "red" in list:
do_something()
do_something()
List Reverse
The reverse method reverses the order of the entire list.
print L1
for i in L1[::-1]:
print i
#OR
L.reverse()
print L
numbers = [5, 1, 4, 3, 2, 6, 7, 9]
print sorted(numbers)
print numbers
print (sorted(my_string))
list.sort()
List Split
Split each element in a list.
mylist = "one;two;three;four,five;1;2"
newlist = mylist.split(';')
print (newlist)
List Indexing
Each item in the list has an assigned index value starting from 0.
list[0] == "first"
list[1] == "second"
list[2] == "third"
List Slicing
Accessing parts of segments is called slicing.
Lists can be accessed just like strings by using the [ ] operators.
The key point to remember is that the :end value represents the first value
that is not in the selected slice.
So, the difference between end and start is the number of elements selected
(if step is 1, the default).
>>> yellow
There is also the step value, which can be used with any of the above
The other feature is that start or end may be a negative number, which means it
counts from the end of the array instead of the beginning.
List Loops
When using loops in programming, you sometimes need to store the results of the
loops.
matching = []
do something
#For example, you can add an if statement in the loop, and add the item to the
(empty) list
if it's matching.
if test(term):
matching.append(term)
#If you already have items in a list, you can easily loop through them like this:
items = [ 1, 2, 3, 4, 5 ]
for i in items:
print (i)
List pop
The pop() method removes the specified index, (or the last item if index is
not specified):
List constructor
Using the list() constructor to make a List:
List count
The count() method returns the number of elements with the specified value.
points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = points.count(9)
List copy
The copy() method returns a copy of the specified list.
x = fruits.copy()
List Methods
Calls to list methods have the list they operate on appear before the method name.
Any other values the method needs to do its job is provided in the normal way as
List Examples
i=0
while i < len(list): #The while loop will print each element in the list
print list[i] #Each element is reached by the index (the letter in the
square bracket)
i=i+1 #Increase the variable i with 1 for every time the while
loop runs.
The next example will count the average value of the elements in the list.
list = [1,2,3,5,8,2,5.2]
total = 0
i=0
i=i+1
print average
List Comprehensions
List comprehensions provide a concise way to create lists.
zero or more for or if clauses. The expressions can be anything, meaning you can
The result will be a new list resulting from evaluating the expression in the
new_list = []
for i in old_list:
if filter(i):
new_list.append(expressions(i))
if conditional:
expression
The * operator is used to repeat. The filter part answers the question if the
x = [i for i in range(10)]
print x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sample problems
def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
print(sum_list([1,2,-8]))
def sort_list_last(tuples):
return sorted(tuples, key=last)
print(sort_list_last([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]))
a = [10,20,30,20,10,50,60,40,80,50,40]
dup_items = set()
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)
Write a Python program to print the numbers of a specified list after removing
even numbers from it.