Computer Science, asked by vardaanupadhyay, 7 months ago

Differentiate between Aliasing and Cloning in lists by taking suitable examples. Write a

Write a Python program to find the highest 4 values in a dictionary​

Answers

Answered by freedarajesh2003
0

Answer:

Explanation:

As assignments are concerned, there is a difference between two types of data: .Containers (and other complex data): Images, lists, sets and dictionaries When you make an assignment for simple data types, their value is copied to the In this example, since names[0] is also a list, the variable name becomes an alias ...

Answered by Tulsi4890
0

Aliasing and cloning are two different ways of creating copies of lists in Python.

Aliasing:

  • Aliasing means creating a new reference to an existing list.
  • In other words, two or more variables pointing to the same list object in memory.
  • When changes are made to one variable, it also affects the other variables pointing to the same list.

Example:

list1 = [1, 2, 3]

list2 = list1 # Aliasing

list1[0] = 0

print(list2) # Output: [0, 2, 3]

Cloning:

  • Cloning means creating a new list with the same elements as an existing list.
  • This can be done using slicing, the copy() method, or the list() constructor.
  • When changes are made to the cloned list, it does not affect the original list.

Example:

list1 = [1, 2, 3]

list2 = list1[:] # Cloning using slicing

list1[0] = 0

print(list2) # Output: [1, 2, 3]

Python program to find the highest 4 values in a dictionary:

def top_values(dictionary):

return sorted(dictionary.values(), reverse=True)[:4]

# Example dictionary

d = {'a': 10, 'b': 5, 'c': 20, 'd': 15, 'e': 25}

print(top_values(d)) # Output: [25, 20, 15, 10]

Explanation:

  • The top_values() function takes a dictionary as input.
  • It extracts the values of the dictionary using the values() method and converts them into a list.
  • The sorted() function sorts the list in descending order.
  • The [:4] slice extracts the top 4 values from the sorted list.
  • The function returns the top 4 values.
  • In the example, the function is called with the dictionary d and the output is the top 4 values in descending order, [25, 20, 15, 10].

To learn more about Python from the given link.

https://brainly.in/question/16086632

#SPJ6

Similar questions