Found 10784 Articles for Python

Combining values from dictionary of list in Python

Pradeep Elance
Updated on 20-May-2020 10:26:12

428 Views

Let’s say we have a python dictionary which has lists as it values in the key value pairs. We need to create a list which will represent all possible combinations of the keys and values from the given lists.With sorted and productThe product function from itertools can be used to create a crtesian product of the iterable supplied to it as parameter. We sort the dictionary and use two for loops to create the combination of all possible key value pairs from the lists in the dictionary.Example Live Demoimport itertools as it Adict = {    "Day": ["Tue", "Wed"],    "Time": ... Read More

Combining two sorted lists in Python

Pradeep Elance
Updated on 20-May-2020 10:21:37

220 Views

Lists are one of the most extensively used python data structures. In this article we will see how to combine the elements of two lists and produce the final output in a sorted manner.With + and sortedThe + operator can join the elements of two lists into one. Then we apply the sorted function which will sort the elements of the final list created with this combination.Example Live DemolistA = ['Mon', 'Tue', 'Fri'] listB = ['Thu', 'Fri', 'Sat'] # Given lists print("Given list A is : ", listA) print("Given list B is : ", listB) # Add and sort res = ... Read More

Combining tuples in list of tuples in Python

Pradeep Elance
Updated on 20-May-2020 10:20:36

468 Views

For data analysis, we sometimes take a combination of data structures available in python. A list can contain tuples as its elements. In this article we will see how we can combine each element of a tuple with another given element and produce a list tuple combination.With for loopIn the below approach we create for loops that will create a pair of elements by taking each element of the tuple and looping through the element in the list.Example Live DemoAlist = [([2, 8, 9], 'Mon'), ([7, 5, 6], 'Wed')] # Given list of tuple print("List of tuples : ", Alist) # ... Read More

Checking triangular inequality on list of lists in Python

Pradeep Elance
Updated on 20-May-2020 10:15:50

234 Views

The sum of two sides of a triangle is always greater than the third side. This is called triangle inequality. Python list of lists we will identify those sublists where the triangle inequality holds good.With for and >We will first get all the sublists sorted. Then for each sublist we will check if the if the sum of first two elements is greater than the third element.Example Live DemoAlist = [[3, 8, 3], [9, 8, 6]] # Sorting sublist of list of list for x in Alist:    x.sort() # Check for triangular inequality for e in Alist:    if e[0] ... Read More

Checking if starting digits are similar in list in Python

Pradeep Elance
Updated on 20-May-2020 10:09:50

258 Views

Sometimes in a given Python list we may be interested only in the first digit of each element in the list. In this article we will check if the first digit of all the elements in a list are same or not.With set and mapSet in Python does not allow any duplicate values in it. So we take the first digit of every element and put it in a set. If all the digits are same then the length of the set will be only 1 has no duplicates allowed.Example Live DemoAlist = [63, 652, 611, 60] # Given list print("Given ... Read More

Check whether a string is valid JSON or not in Python

Pradeep Elance
Updated on 20-May-2020 10:04:16

1K+ Views

JSON is a type of text format use to exchange data easily between various computer programs. It has a specific format which Python can validate. In this article we will consider a string and using JSON module we will validate if the string represents a valid JSON format or not.Creating JSON ObjectThe json module has method called loads. It loads a valid json string to create a Json object. In this example we load the string and check that there is no error in loading the JSON object. If there is error we consider the JSON string as invalid.Example Live Demoimport ... Read More

Insert the string at the beginning of all items in a list in Python

Mohd Mohtashim
Updated on 16-May-2020 10:50:38

264 Views

In this post we need to enter the string at the beginning of all items in a list. For ex: We're given string = "Tutorials_Point" and List contains multiple element such as "1", "2" etc. So in this we need to add Tutorials_Point in front of "1", "2" and so on.ExampleAproach 1 Live Demosample_list = [1, 2, 3] print(['Tutorials_Point{0}'.format(i) for i in sample_list])Output//['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3']Approach 2 Live Demosample_list = [1, 2, 3] sample_str = 'Tutorials_Point' sample_str += '{0}' sample_list = ((map(sample_str.format, sample_list))) print(sample_list)Output//['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3']Read More

Python - int() function

Mohd Mohtashim
Updated on 16-May-2020 10:45:43

3K+ Views

Python int() function converts the specified value into an integer number.The int() function will returns an integer object constructed from a number or string let say x, or return 0 if no argum ents are specified.Syntaxint(value, base) int(x, base=10)value = A number or a string that can be converted into an integer numberbase = A number representing the number format. Default value − 10Example# int() for integers int(10) 10 int(20) 20 # int() for floating point numbers int(11.2) 11 int(9.9e5) 990000

Python - Insert list in another list

Mohd Mohtashim
Updated on 16-May-2020 10:39:54

238 Views

A list also the extend() method, which appends items from the list you pass as an argument. extend() − Python list method extend() appends the contents of seq to list. You can read more about it here "https://www.tutorialspoint.com/python/list_append.htm"Now let us see hands onExample Live Demo#append first_list = [1, 2, 3, 4, 5] second_list = [6, 7, 8, 9, 10] first_list.append(second_list) # print result using append print("The list pattern using append is : " + str(first_list)) #extend third_list_ = [11, 12, 13, 14, 15] fourth_list = [16, 17, 18, 19, 20] third_list_.extend(fourth_list) print("The list pattern using extend is : " + str(third_list_))OutputThe ... Read More

Python - Increasing alternate element pattern in list

Mohd Mohtashim
Updated on 16-May-2020 10:37:27

100 Views

In this we are going to use List Comprehension with enumerate() Python provides compact syntax for deriving one list from another. These expressions are called list comprehensions.List comprehensions are one of the most powerful tools in Python. Python’s list comprehension is an example of the language’s support for functional programming concepts. You can read more about it here "www.tutorialspoint.com/python-list-comprehension" The enumerate() method adds counter to the iterable. You can read more about enumerate here " www.tutorialspoint.com/enumerate-in-python"Example Live Demo# declare list of integers my_list = [1, 2, 3] # printing the value print("Printing my_list list : " + str(my_list)) response = [value ... Read More

Advertisements