# Python Lists
In [2]: # Reverse a list
list = [1,2,3,"harish",5]
list.reverse()
print(list)
[5, 'harish', 3, 2, 1]
In [3]: # Another method
list[::-1]
print(list)
[5, 'harish', 3, 2, 1]
In [5]: # Concatenate two lists index-wise
list1 = ['M','Na','i','Har']
list2 = ['y','me','s','ish']
list3 = [i+j for i,j in zip(list1,list2)]
print(list3)
['My', 'Name', 'is', 'Hari', 'sh']
In [6]: # By using '+' operator
list1 = [1,2,3,4]
list2 = [5,6,7,8]
res = list1+list2
print(res)
[1, 2, 3, 4, 5, 6, 7, 8]
In [7]: # using for loop
list1 = [1,2,3,4]
list2 = [5,6,7,8]
print("List1 before Concatenation:\n" + str(list1))
for x in list2 :
list1.append(x)
print ("Concatenated list i.e. list1 after concatenation:\n" + str(list1))
List1 before Concatenation:
[1, 2, 3, 4]
Concatenated list i.e. list1 after concatenation:
[1, 2, 3, 4, 5, 6, 7, 8]
In [8]: # by using list comprehension
list1 = [10, 11, 12, 13, 14]
list2 = [20, 30, 42]
res = [j for i in [list1, list2] for j in i]
print ("Concatenated list:\n"+ str(res))
Concatenated list:
[10, 11, 12, 13, 14, 20, 30, 42]
In [12]: # By using extend method
l1 = [1,2,3,4]
l2 = [5,6,7,8]
print(l1.extend(l2))
print(l1)
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
None
[1, 2, 3, 4, 5, 6, 7, 8]
In [13]: # by using '*' operator
l1 = [1,2,3,4]
l2 = [5,6,7,8]
res = [*list1, *list2]
print(res)
[1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8]
In [14]: # turn every item of a list into its square.
list = [1,2,3,4,5,6,7]
res = []
for i in list:
res.append(i*i)
print(res)
[1, 4, 9, 16, 25, 36, 49]
In [15]: # by using list comprehension
list = [1,2,3,4,5,6,7]
list = [x*x for x in list]
print(list)
[1, 4, 9, 16, 25, 36, 49]
In [16]: # concatenate 2 lists in the following order
#list1 = ["Hello ", "take "]
#list2 = ["Dear", "Sir"]
#o/p:['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
res = [x + y for x in list1 for y in list2]
print(res)
['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
In [17]: # iterate both lists simultaneously and display items from list1 in original order and i
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
for x, y in zip(list1, list2[::-1]):
print(x, y)
10 400
20 300
30 200
40 100
In [18]: # Add new item to list after a specified item
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
# understand indexing
# list1[0] = 10
# list1[1] = 20
# list1[2] = [300, 400, [5000, 6000], 500]
# list1[2][2] = [5000, 6000]
# list1[2][2][1] = 6000
# solution
list1[2][2].append(7000)
print(list1)
[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
In [19]: # Extend nested list by adding the sublist
list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "l"], "m", "n"]
sub_list = ["h", "i", "j"]
# understand indexing
# list1[2] = ['c', ['d', 'e', ['f', 'g'], 'k'], 'l']
# list1[2][1] = ['d', 'e', ['f', 'g'], 'k']
# list1[2][1][2] = ['f', 'g']
# solution
list1[2][1][2].extend(sub_list)
print(list1)
['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']
In [23]: # Replace list’s item with new value if found
list1 = [5, 10, 15,45,20,45, 25, 50, 20]
# get the first occurrence index
index = list1.index(45)
print(index)
# update item present at location
list1[index] = 200
print(list1)
3
[5, 10, 15, 200, 20, 45, 25, 50, 20]
In [24]: #
letters = [ letter for letter in 'human' ]
print(letters)
['h', 'u', 'm', 'a', 'n']
In [25]: #
list = [1,2,3,4,5]
list1 = [x*x for x in list]
print(list1)
[1, 4, 9, 16, 25]
In [26]: matrix = [[1,2,3,5], [4,5,6,8], [7,8,9,13], [10,11,12,14]]
transpose = [[row[i] for row in matrix] for i in range(4)]
print (transpose)
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12], [5, 8, 13, 14]]
In [27]: num_list = [y for y in range(100) if y%2 == 0 if y%4 == 0]
print(num_list)
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 8
8, 92, 96]
In [28]: l = ["even" if x%2==0 else "odd" for x in range(20)]
print(l)
['even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'od
d', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd']
In [29]: x = set()
print(x)
set()
In [30]: type(x)
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
set
Out[30]:
In [32]: n = set([1,2,3,4])
print(n)
{1, 2, 3, 4}
In [33]: a = {1,2,'a','b',4}
print(a)
{1, 2, 'b', 4, 'a'}
In [34]: type(a)
set
Out[34]:
In [35]: #Create a set
num_set = set([0, 1, 2, 3, 4, 5])
for n in num_set:
print(n, end=' ')
print("\n\nCreating a set using string:")
char_set = set("w3resource")
# Iterating using for loop
for val in char_set:
print(val, end=' ')
0 1 2 3 4 5
Creating a set using string:
r 3 w c s u e o
In [ ]:
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js