Python Articles

Page 604 of 852

How to sort a dictionary in Python?

pawandeep
pawandeep
Updated on 11-Mar-2021 5K+ Views

A dictionary is a data structure that consists of key and value pairs. We can sort a dictionary using two criteria −Sort by key − The dictionary is sorted in ascending order of its keys. The values are not taken care of.Sort by value − The dictionary is sorted in ascending order of the values.Method 1 − Sort the dictionary by keyIn this approach, the dictionary is sorted in ascending order of its keys.Input:{2:90, 1: 100, 8: 3, 5: 67, 3: 5}Output:{1:100, 2:90, 3:5, 5:67, 8:3}As shown above, we can see the dictionary is sorted according to its keys.Example Live Demodic={2:90, ...

Read More

Rear element extraction from list of tuples records in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2021 1K+ Views

If it is required to extract the rear element from a list of tuples, it can be done using list comprehension and negative indexing.The list comprehension is a shorthand to iterate through the list and perform operations on it.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.Below is a demonstration of the same −ExampleLive Demomy_list = [('Will', 67, 45), ('Jam', 34, 56), ('Pow', 99, 123), ('Nyk', 0, 56)] print ("The list of tuples is : ...

Read More

Cummulative Nested Tuple Column Product in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2021 255 Views

If it is required to find the cumulative column product of a nested tuple, the 'zip' method and a nested generator expression can be used.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration of the same −ExampleLive Demotuple_1 = ((11, 23), (41, 25), (22, 19)) tuple_2 = ((60, 73), (31, 91), ...

Read More

Python program to sort a list of tuples alphabetically

AmitDiwan
AmitDiwan
Updated on 11-Mar-2021 2K+ Views

When it is required to sort a list of tuples in alphabetical order, the 'sort' method can be used. When using this, the contents of the original tuple get changed, since in-place sorting is performed.The 'sort' function sorts the values in ascending order by default. If the order of sorting is specified as descending, it is sorted in descending order.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.Below is a demonstration of the same −ExampleLive Demodef ...

Read More

How long does it take to learn Python?

pawandeep
pawandeep
Updated on 11-Mar-2021 373 Views

How fast can someone learn or excel in something completely depends on one’s interest in learning, dedication and persistence.Now, how long does it take to learn Python depends on how much do you want to learn.The basics of Python which include functions, loops, conditional statements, datatypes, etc., would take about one to two weeks on average for a newbie. Again, this depends on how much time do you devote and your persistence in learning.If you aspire to learn Python in detail, it would take few months to learn Python. There is no end to learning Python as it is too ...

Read More

Is Python a scripting language?

pawandeep
pawandeep
Updated on 11-Mar-2021 6K+ Views

Yes, Python is a scripting language.Scripting language v/s Programming languageThe first question which strikes into the mind is, what is the difference between programming and scripting language. The only difference which exists is that the scripting language does not require any compilation, it is directly interpreted.For example, the programs written in a language such as C++ are compiled before execution whereas the programs written in scripting languages such as Python or JavaScript are directly interpreted and are not compiled.Why is Python a scripting language?A scripting language is one that is interpreted. Python is an interpreted language. Python uses an interpreter ...

Read More

Kth Column Product in Tuple List in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2021 270 Views

When it is required to find the 'K'th column product in a list of tuple, a simple list comprehension and a loop can be used.A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.The list comprehension ...

Read More

Remove nested records from tuple in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2021 410 Views

When it is required to remove nested record/tuples from a tuple of tuple, a simple loop and the 'isinstance' method and the enumerate method can be used.The enumerate method adds a counter to the given iterable, and returns it. The 'isinstance' method checks to see if a given parameter belong to a specific data type or not.Below is a demonstration of the same −ExampleLive Demotuple_1 = (11, 23, (41, 25, 22), 19) print("The tuple is : ") print(tuple_1) my_result = tuple() for count, elem in enumerate(tuple_1):    if not isinstance(elem, tuple):       my_result = my_result + ...

Read More

Write a program to form a cumulative sum list in Python

pawandeep
pawandeep
Updated on 10-Mar-2021 3K+ Views

The cumulative sum till ith element refers to the total sum from 0th to ith element.The program statement is to form a new list from a given list. The ith element in the new list will be the cumulative sum from 0 to ith element in the given list.For example, Input[10, 20, 30, 40, 50]Output[10, 30, 60, 100, 150]Input[1, 2, 3, 4, 5]Output[1, 3, 6, 10, 15]Following is a program to form a cumulative sum list using the input list −The input list is passed to the function cumSum() which returns the cumulative sum list.We declare an empty list cum_list ...

Read More

How to print pattern in Python?

pawandeep
pawandeep
Updated on 10-Mar-2021 2K+ Views

Patterns in Python can be printed using nested for loops. The outer loop is used to iterate through the number of rows whereas the inner loop is used to handle the number of columns. The print statement is modified to form various patterns according to the requirement.The patterns can be star patterns, number patterns, alphabet patterns. The patterns can be of different shapes, triangle, pyramids, etc.ExampleAll these patterns can be printed with the help of for loops with modified print statements which forms these different patterns.The basic idea between the printing of these patterns is the same with slight differences.We ...

Read More
Showing 6031–6040 of 8,519 articles
« Prev 1 602 603 604 605 606 852 Next »
Advertisements