Creating List
Creating List
1 Empty list: []
2 With Elements: [2] OR [2,1,5,3]
3 List comprehension: It is way to create another list from original list based on conditions.
Syntax: new_list=[expression for item in iterable condition_of_true]
if lst=[1,5,2,7]
I. Mutable: (Changes elements of original list)
1 append: (Appends value at end of list)
lst.append(10)
2 Extend: (To append list at end of other list)
lst.extend([2,4,1])
3 insert: (inserts an element at an index location passed)
lst.insert(10,'val')
4 del: (deletes value(s) from string by passing index/deletes list and freeup memory)
del x deletes x list from memory
del x[1] deletes 2nd element from list -> [5,2,7]
del x[:1] deletes 1st element from list -> [5,2,7]
del x[2:] deletes elements from 2nd index till last index
del x[] invalid. Put index values when dealing with [ & ]
del x[:] It is same as empty list i.e. []. Deletes all elements of list starting from 1s
5 remove: (removes 1st match from list) (remove element by value)
lst.remove(5) this will return [1,2,7]
6 reverse: (reverses whole list)
lst.reverse() returns [7,2,5,1]
7 sort: (sorts elements in ascending order by default. Use 'revers' option to reverse in descending ord
lst.sort() returns [1,2,5,7]
lst.sort(reverse=True) returns [7,5,2,1]. Reverse sorting.
lst.sort(reverse=False) this is similar to lst.sort(). Ascending order.
Custom sorting:(sort based on length of strings)
def srt(str):
return len(str)
a=['dsd','ewwewe','a']
a.sort(key=srt)
print(a)
Output: ['a', 'dsd', 'ewwewe']
II. Immutable: (Does not change element of original list)
1 sorted: (similar to sort(), but does not change original list.) (all options of sort() can be applied to so
sorted(lst) sorts list in ascending order
sorted(lst,reverse=True) sorts list in descending order
Custom sort:
def srt_str(val):
return len(val)
x=['sssd','qe','sdsdsdss']
print(sorted(x,key=srt_str))
This will sort list based on logic written in function
2 +: (to concatenate/join 2 lists) (to concatenate lists of same data type)
x=[1,2,3]
b=[3,2,1]
print(a+b) it returns [1,2,3,3,2,1]
3 *: (replicates list number of times)
print(lst*2) this prints lst list 2 times i.e. [1,5,2,7,1,5,2,7]
4 min: (retruns value from list having minimum value)
print(min(lst)) prints -> 1
5 max: (returns max value from list)
print(max(lst)) prints->7
6 index: (returns position of 1st occurrence of an element from list)
print(lst.index(5)) prints index value-> 1
7 count: (returns number of time a value present in list)
print(x.count(5)) prints -> 1
8 sum: (returns sum of values from list)
print(sum(lst)) prints 15
9 in: (finds if an element is present in list) (Returns only boolean value)
print(10 in lst) prints False. Because 10 is not in list lst.
Observations on list:
print(lst[0]) 1
print(lst[0:0]) Empty list []
print(lst[0:1]) 1
print(lst[:1]) 1
print(lst[:]) All elements [1,5,2,7]
print(lst[:6]) Note that there are only 4 elements in list. But this is not error. Valid. But still it prin
Note: for lst list length is 4. So while slicing if indexes are too -ve or too +ve then there is no error.
e.g.
print(lst[-5:]) valid and prints all elements
print(lst[0:-10]) valid and prints all elements
eeup memory) (first index is w.r.t. 0 & 2nd index passed is w.r.t.1) (remove element by index)
nding order.
ns of sort() can be applied to sorted())
ed on logic written in function to calculate length. This will sort list based on length of elements in ascending order.
lst[index_1,index_2] (index_1 should always be less than index_2)
Slicing tuple: Slicing tuple returns a new tuple with subset of original tuple.
print(tpl[0]) 1
print(tpl[0:0]) Empty tuple ()
print(tpl[0:1]) 1
print(tpl[:1]) 1
print(tpl[:]) All elements (1,5,2,7)
print(tpl[:6]) Note that there are only 4 elements in list. But this is not error. Valid. But
Note: for tpl tuple length is 4. So while slicing if indexes are too -ve or too +ve then there is no e
e.g.
print(tpl[-5:]) valid and prints all elements
print(tpl[0:-10]) valid and prints all elements
We cannot change values of existing tuple. Because they are immutable. Assigning new value to existing tuple value results in
However, we can only create new tuples per our requirement.
--Not a tuple
But this is not error. Valid. But still it prints all elements of list.
Here, enumerate() function returns a tuples having index & actual value in it.