Found 10476 Articles for Python

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

323 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

158 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

Find common elements in list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:54:18

2K+ Views

It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this.With map and intersectionIntersection is a simple mathematical concept of finding the common elements between different sets. Python has the set method which returns a set that contains the similarity between two or more sets. So we first convert the elements of the list into set through a map function and then apply the ... Read More

Convert list into list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:52:01

3K+ Views

During data analysis we face scenarios to convert every element of a list into a sublist. So in this article we will need to take a normal list as an input and convert into a list of lists where each element becomes a sublist.Using for loopThis is a very straight forward approach in which we create for loop to read each element. We read it as a list and store the result in the new list.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] #Given list print("Given list: ", Alist) # Each element as list NewList= [[x] for x ... Read More

Convert dictionary to list of tuples in Python

Pradeep Elance
Updated on 13-May-2020 14:49:30

856 Views

Converting from one collection type to another is very common in python. Depending the data processing needs we may have to convert the key value pairs present in a dictionary to pairs representing tuples in a list. In this article we will see the approaches to achieve this.With inThis is a straight forward approach where we just consider theExample Live DemoAdict = {30:'Mon', 11:'Tue', 19:'Fri'} # Given dictionary print("The given dictionary: ", Adict) # Using in Alist = [(key, val) for key, val in Adict.items()] # Result print("The list of tuples: ", Alist)OutputRunning the above code gives us ... Read More

Check if two lists are identical in Python

Pradeep Elance
Updated on 13-May-2020 14:47:00

614 Views

In python data analysis, we may come across situation when we need to compare two lists and find out if they are identical meaning having same elements or not.Exmple Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu'] listB = ['Mon', 'Wed', 'Tue', 'Thu'] # Given lists print("Given listA: ", listA) print("Given listB: ", listB) # Sort the lists listA.sort() listB.sort() # Check for equality if listA == listB:    print("Lists are identical") else:    print("Lists are not identical")OutputRunning the above code gives us the following result −Given listA: ['Mon', 'Tue', 'Wed', 'Thu'] Given listB: ['Mon', 'Wed', 'Tue', 'Thu'] Lists are identicalWith ... Read More

Check if substring present in string in Python

Pradeep Elance
Updated on 13-May-2020 14:44:33

263 Views

In python data analysis we may come across a scenario to check if a given substring is part of a bigger string. We will achieve this through the following programs.With findThe find function finds the first occurrence of the specified value. If the value is not found then it returns -1. We will apply this function to the given string and design a if clause to find out is substring is part of a string.Example Live DemoAstring = "In cloud 9" Asub_str = "cloud" # Given string and substring print("Given string: ", Astring) print("Given substring: ", Asub_str) if (Astring.find(Asub_str) == -1): ... Read More

Check if one list is subset of other in Python

SaiKrishna Tavva
Updated on 14-Oct-2024 14:11:38

5K+ Views

Python provides various methods to check if one list is a subset of another like 'all()' function and also by using 'issubset()' function to perform this check effectively. The three primary approaches used for checking if one list is a subset of another in Python are as follows. all() function : Checks if all elements in list of an iterable are true. issubet() method : Used in python sets for collecting of unique elements. intersection() method : ... Read More

Check if list is strictly increasing in Python

Pradeep Elance
Updated on 13-May-2020 14:39:04

1K+ Views

Given a list, we may need to check for the sequence of its elements. In this article we will find out if the elements present in the list are in a strictly increasing order. Below programs achieve that objective.With all and zipIn this approach we first slice each element compare its value to the next element that is sliced. If all such comparisons hold true then we conclude the list is strictly in increasing order.Example Live DemolistA = [11, 23, 42, 51, 67] #Given list print("Given list : ", listA) # Apply all and range if (all(i < j for i, ... Read More

Advertisements