Python program: “List” related Questions

Data types : 

  1. Numbers
  2. Strings
  3. List
  4. Dictionary 
  5. Tuple
  6. Set

Numbers in Python: 

Integer, floating point numbers, Complex, Boolean.

Strings in Python:

 A string is a sequence of characters. In Python single, double, and triple quotes are used to define strings.

List in Python: 

Python list is an ordered sequence of items. We can store more than one item of different types in a list. Items in the list are accessed using an index.

Tuple in Python: 

Tuple is an ordered sequence of items same as list. The difference is that tuples are immutable and list is mutable.

Python Set : 

Set is an unordered collection of unique items, which means duplicates are not allowed. Set is defined using {} and items in set are separated by commas. set items are not ordered. Set items can’t be accessed using index as they are unordered, hence ‘indexing’ is NOT possible here. Repeated value is shown only ONCE. values gets sorted out

Dictionary in Python:

Is an unordered collection of key-value pairs. Is defined using {} and each item is a pair of keys and value.

Q: Break list into n chunks

alpha_list = [34, 23, 67, 100, 88, 2,15,23,8,19,91,34,21,0,45,21,72,32,4
n = 4
out = [alpha_list[i:i+n] for i in range(0,len(alpha_list),n)]
print(out)]        


Q: Find the index of an item in a specified list

ip_lst = ['Red', 'Yellow','Green', 'White', 'Black', 'Pink', 'Yellow'


def specific_index_list(ip_lst, input_item):
    count=-1
    for i in ip_lst:
        count+=1
        if i == input_item:
            break
    return "index of {} is {}".format(input_item,count)
specific_index_list(ip_lst,'Yellow')]        


Q: Split a list in N parts.

l = [1,2,3,4,5,6,7,8,9, 10
def custom_split_element(in_list, element_split):
    return [in_list[i::element_split] for i in range(element_split)]
custom_split_element(l,2)]        


Q: Write a Python program to multiply all the items in a list

lst = [1,3,17,-1
mlt = 1
for i in range(len(lst)):
    mlt=mlt*lst[i]


print(mlt)]        


Q: Write a Python program to multiply all the items in a list.

mlt = 
for i in range(2,5):
    mlt *= i
print(mlt)1        


Q: Write a Python program to get the largest number from a list.

lst = [10, 20, 4, 45, 99
print(max(lst))
lst.sort()
print("Number is:", lst[-1])]        


Q: Find the k-th largest number in a sequence of unsorted numbers

def kLargest(arr, k)
    arr.sort(reverse = True)
    print(arr[k-1]) 


    # Print the first kth largest elements
    for i in range(k):
        print (arr[i], end =" ")
nums = [56,14,7,98,32,12,11,50,45,78,7,5,69]
k = 3 
kLargest(nums,k):        


Q: Count number of strings where the string length is 2 or more and first and last characters are same

nums = ['abc', 'xyz', 'aba', '1221'
count = 0
for i in nums:
    if len(i)>=2 and i[0] == i[-1]:
        count += 1
print(count) #'aba', '1221']        


Q: #convert list to dictionary

name = ['Suresh', 'Ramesh', 'Mahesh', 'Vikas', 'Prakash', 'Ram','Sita'
surname = ['Ram', 'Kumar', 'Paswan', 'Kumar', 'Padukone', 'Sita ','Singh']


d = {}
for i in name:
    for j in surname:
        d[i] = j
print(d)]        


Q: Convert list of tuples into Dict

tups = [('vikas',30),('Gaurav',21),('Shaurya',99),('Siddhant',35)
tups
dict(tups)]        


Q: Given a list of numbers, where every number shows up twice except for one number, find that one number

list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
 
set_res = set(list_inp) 
print("The unique elements of the input list using set():\n") 
list_res = (list(set_res))
  
for item in list_res: 
    print(item)          


Q: Get the frequency of the elements in a list

lst = [1,1,2,2,3,3,4,4,5,5,5,6,6,6,88


l = []
for j in set(lst):
    l.append((j,lst.count(j)))
print(l)]        


Q: Print a list of first and last 5 element’s square in range of numbers between 1 and 30 (both included).

lst = [i for i in range(1,31)
l = []
for j in lst[:5]+lst[-5:]:
    l.append(j**2)
print(l)]        


Q: To check whether a list contains a sub list

lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7
l = []
for i in lst:
    if type(i)== list:
        l.append(i)
print(l) ]        

To view or add a comment, sign in

More articles by Vikas K.

  • Interview of the Day

    Solve the problem and share your response Date: 5 January Topic: Decreasing Comments. ID: 512025 Asked by: Meta.

  • Reading and Writing JSON Files in Python

    Interview Question: Write a Python script to process a JSON file containing customer data and convert it into a…

  • Using different models: Detect Objects and Label them

    Object Detection, Classification, and Captioning 1. Using "Image classification", predict which class(es) (i.

  • Deloitte's New Python Challenge

    Problem Statement: Calculate the average score for each project, but only include projects where more than one team…

  • Know about GitHub Copilot

    Watch "GitHub Copilot sessions": Session 1: GitHub Copilot: A Journey of Discovery | Session 1: Latest Innovations &…

  • Know about GitHub Copilot

    Why GitHub Copilot: 1. Reduce Repetitively 2.

  • Find unique words from a text file

    From a sentence or paragraph, find the unique words after removing stopwords and cleaning the data Consider a text…

  • File Handling

    Q: Write a program that takes a sentence/paragraph as input and counts the number of words in it. Find the total word…

  • Know about Microsoft Azure Cognitive Search

    Azure Cognitive Search/Azure AI Search Fully managed; Built-in AI; Customizable; Secure and Compliant -- When you…

    1 Comment
  • Sample code for a ChatGPT prompt design:

    Output is: Title: Tuberculosis Unveiled: A Poetic Journey Introduction: In the realm of health, a silent foe resides…

    1 Comment

Insights from the community

Others also viewed

Explore topics