Some Common Python List and Tuple Questions

Some Common Python List and Tuple Questions

Is Python list mutable

Yes, Python list are mutable data types i.e., we can change, add, remove any value at any line of code once defined.

name = ["Rahul", "Sachin", "Shivam"]
name[0] = "Ritik"

Does a list need to be homogeneous

No, It is not needed for a list to be homogeneous. We can store any type of data in the list.

list_1 = ["Rahul", 3, "Sachin", "Shivam", 56]

What is the difference between a list and a tuple

List is a mutable data type while Tuple is not a mutable data type.

How to find the number of elements in the list

Using len() function we can find the number of elements in the list.

name = ["Rahul", "Sachin", "Shivam"]
len(name)

How to check whether the list is empty or not

If len() function returns 0 then list is empty

name = [ ]
length = len(name)
if length == 0:
  print("List is empty")
else:
  print("List is not empty")

How to find the first and last element of the list

For first element <List_name[0]> and for last element <List_name[-1]>

name = ["Rahul", "Sachin", "Shivam"]
first = name[0]
last = name[-1]

How to find the largest and lowest value in the list

Using max() and min() function we can find the largest and lowest value in the list

score = [35, 64, 77, 24, 96]
maximum = max(score)
minimum = min(score)

How to access elements of the list

We can access any element of list by using <list_name[index]>

name = ["Rahul", "Sachin", "Shivam"]
name[0]
name[1]

Remove elements in a list before a specific index

Using pop() function we can remove elements in a list before a specific index python

name = ["Rahul", "Sachin", "Shivam"]
name.pop(1)

Remove elements in a list between 2 indices

Using del function we can remove elements in a list between 2 indices

name = ["Rahul", "Sachin", "Shivam"]
del name[1:2]
name

Return every 2nd element in a list between 2 indices

score = [35, 64, 77, 24, 96]
score[0:4:2]

Get the first element from each nested list in a list

score = [[35, 64, 77], [24, 96,34]]
for i in score:
  print(score[0])

How to modify elements of the list

name = ["Rahul", "Sachin", "Shivam"]
name[0] = "Ritik"

How to concatenate two lists

name = ["Rahul", "Sachin", "Shivam"]
score = [35, 64, 77, 24, 96]
for i in score:
  name.append(i)

or

name = ["Rahul", "Sachin", "Shivam"]
score = [35, 64, 77, 24, 96]
name.extend(score)

How to add two lists element-wise in python

score_1 = [43, 74, 23, 88, 93]
score_2 = [35, 64, 77, 24, 96]
sum = [ ]
for i,j in zip(score_1,score_2):
  sum.append(i+j)

Difference between del and clear

del function helps to delete the list variable from code while clear function clear the all elements present in the list without delete its variable

Difference between remove and pop

remove function helps to delete the first occurrence of the number or string mentioned in its arguments while pop function helps to delete the individual element according to positioning of the list

Difference between append and extend

append() adds one element at a time while extend() can add number of elements at a time

Difference between indexing and Slicing

Indexing means referring to an element of an list by its position while Slicing means getting a subset of elements from an list based on their indices

Difference between sort and sorted

list_name.sort() function sorts the list and replaces the original list while sorted(list_name) returns a new sorted list without changing the original list

Difference between reverse and reversed

reverse() actually reverses the elements in the container. reversed() doesn't actually reverse anything, it merely returns an object that can be used to iterate over the container's elements in reverse order

Difference between copy and deepcopy

Copy constructs a new compound object and then inserts references into it to the objects found in the original while deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original

How to remove duplicate elements in the list

numbers = [43, 74, 43, 93, 24, 23, 88, 23, 88, 93]
set(numbers)

How to find an index of an element in the python list

numbers = [43, 74, 43, 93, 24, 23, 88, 23, 88, 93]
numbers.index(74)

How to find the occurrences of an element in the python list

numbers = [43, 74, 43, 93, 43, 24, 23, 43, 88, 23, 43, 88, 93, 43]
numbers.count(43)

How to insert an item at a given position

name = ["Rahul", "Sachin", "Shivam"]
name.insert(1, "Ritik")

How to check if an item is in the list

name = ["Rahul", "Sachin", "Shivam"]
if "Rahul" in numbers:
  print("Item is present")

How to flatten a list in python

numbers_2d = [[1,2,3],[4,5,6],[7,8,9]] 
numbers_1d = []

for i in range(len(numbers_2d)): 
  for j in range (len(numbers_2d[i])):
    numbers_1d.append(numbers_2d[i][j]) 

How to convert python list to other data structures like set, tuple, dictionary

numbers = [43, 74, 43, 93, 24, 23, 88, 23, 88, 93]
set(numbers)
tuple(numbers)

How to apply a function to all items in the list

def square(number):
    return number**2

numbers = [1, 2, 3]

final_list = list(map(square, numbers))

How to filter the elements based on a function in a python list

numbers = [15, 20, 57, 87, 66, 02]
pass = filter(lambda score: score >= 30, numbers)

print(list(pass))

How python lists are stored in memory

Python uses a garbage collection algorithm (called Garbage Collector) that keeps the Heap memory clean and removes objects that are not needed anymore.












要查看或添加评论,请登录

Shresth V.的更多文章

社区洞察

其他会员也浏览了