List in Python

#----------------------------- 1- Creating a list ------------------------------- 
#creating a list
Languages = ["Erlang", "Dart", "Julia","Scala","Haskell"]

# Accessing elements by index
print(Languages[2])  
print(Languages[1]) 

# Accessing elements from the end
print(Languages[-1]) 
#----------------------------- 2- Modifying a list ------------------------------- 

# Creating a list

Languages = ["Erlang", "Dart", "Julia","Scala","Haskell"]

# Modifying elements
Languages[1] = "Python"
print(Languages)  # Output: ["Erlang", "Python", "Julia", "Scala", "Haskell"]

# Adding elements to the end
Languages.append("C++")  # x.append(element)
print(Languages)  # Output: ["Eralng", "Dart", "Julia", "Haskell","C++"]

# Removing elements by value
Languages.remove("Scala") # x.remove(element)
print(Languages)  # Output: ["Eralng", "Dart", "Julia", "Haskell"]

# Removing elements by index
del Languages[0]
print(Languages)  # Output: ["Dart", "Julia", "Scala", "Haskell"]
#----------------------------- 3- Slicing list -----------------------------

# Creating a list
Languages = ["Erlang", "Dart", "Julia","Scala","Haskell"]

# Iterating through a list using a for loop
for program in Languages:
    print(program)
print("-------------------------------------------------------------")

# Iterating through a list using a while loop and index
i = 0
while i < len(Languages):
    print(Languages[i])
    i += 1
#----------------------------- 4- Iterating through list -----------------------------

# Slicing a list to get a sublist
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sliced = numbers[3:8]
print(sliced)  # Output: [ 3, 4, 6, 7]
#----------------------------- 5- Comparing lists -----------------------------

# new list with  list comprehension
squares = [x ** 2 for x in range(2, 10)]
#print(squares)
numbers =[4, 9, 16, 25, 36, 49, 64, 81]
#print(numbers)
print(squares==numbers) # True, since both lists  are same
print()

numbers2 = [45, 232, 12, 10, 81, 3]
#print(numbers2)
print()
print(numbers2 == squares) # False, since both lists are same

#---------------------------- 6- Sorting the lists -----------------------------------

# Sorting a list in ascending order

alpha = [“c”, “a”, “d”, “a”, “e”, “h”, “b”, “f”, “t”, “c”, “e”]

alpha.sort()

print(“Original list”)

print(alpha)  # Output: [“a”, “a”, “b”, “c”, “c”, “d”, “e”, “e”, “f”, “h”, “t”]

# Sorting a list in descending order

print(“Reversed List”)

alpha.sort(reverse=True)

print(alpha)  # Output: [ “t”, “h”, “f”, “e”, “e”, “d”, “c”, “c”, “b”, “a”, “a”]
#------------------------------ 7- Concatination of lists ----------------------------

# Combining two lists
list1 = ["W", "e", "b"]
list2 = ["G","a", "p", "p"]
joined = list1 + list2 
print(joined) # output: ['W', 'e', 'b', 'G', 'a', 'p', 'p']
#-------------------------------- 8- Nested lists ------------------------------------ 


# Creating a list of lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Accessing elements in a nested list
print(matrix[2])  # output: [7, 8, 9]    3rd sublist in matrix list
print(matrix[1][2])  # Output: 6  (element in the 2nd matrix sublist and element 2nd  index )
#-------------------------------- 9- Swapping in  lists ------------------------------

# Initializing list
count =0
list = []
num=int(input("Total elements : "))
print("Enter the elements : ")
for i in range(num):
    val=int(input())
    list.append(val)

key = int(input("Enter the key to search : "))
print("Searching for",key,"....")
for i in range(num):
    
    if key == list[i]:
         count +=1
if count ==0:
    print(key,"not found in the list.")
else:
    print("Found",key, "for",count)
	

Join our team, where career growth knows no limits.

For More Details

Our internship program guarantees personalized attention for optimal career development in the tech industry.

+91 97879 68375

-> You should bring your own laptop during the internship

Who can apply?

Internships are open for students who: B.E / B.Tech / M.E / M.Tech / B.Sc / M.Sc / M.C.A / B.C.A / Diploma.

  • Ongoing Students Only
  • days, 5days/ 10days or Based on Student Preference
  • Limited 1 to 10 students per slot
  • Registration fees : 1000 INR
  • Slots are filling quickly only fewer slots are available