Found 10784 Articles for Python

Convert set into a list in Python

Pradeep Elance
Updated on 20-May-2020 11:12:20

242 Views

As part of data analysis using Python we may be required to to convert the data container from set to list. In this article we'll see e how to solve this requirement.With listThis is a straightforward approach in which we directly apply the list function on the given set. The elements get converted into elements of a list.Example Live DemosetA = {'Mon', 'day', '7pm'} # Given Set print("Given set : ", setA) res = (list(setA) ) # Result print("Final list: ", res)OutputRunning the above code gives us the following result −Given set : ['7pm', 'Mon', 'day'] Final list: ['7pm', 'Mon', 'day']With ... Read More

Convert number to list of integers in Python

Pradeep Elance
Updated on 20-May-2020 11:10:01

9K+ Views

As part of data manipulation in Python we may sometimes need to convert a given number into a list which contains the digits from that number. In this article we'll see the approaches to achieve this.With list comprehensionIn the below approach we apply the str function to the given number and then convert into integer through identity function. Finally we wrap the result into a list.Example Live DemonumA = 1342 # Given number print("Given number : ", numA) res = [int(x) for x in str(numA)] # Result print("List of number: ", res)OutputRunning the above code gives us the following result −Given ... Read More

Convert list of tuples to list of strings in Python

Pradeep Elance
Updated on 20-May-2020 11:08:40

532 Views

During data processing using python, we may come across a list whose elements are tuples. And then we may further need to convert the tuples into a list of strings.With joinThe join() returns a string in which the elements of sequence have been joined by str separator. We will supply the list elements as parameter to this function and put the result into a list.Example Live DemolistA = [('M', 'o', 'n'), ('d', 'a', 'y'), ('7', 'pm')] # Given list print("Given list : ", listA) res = [''.join(i) for i in listA] # Result print("Final list: ", res)OutputRunning the above code gives ... Read More

Convert list of tuples to list of list in Python

Pradeep Elance
Updated on 20-May-2020 11:06:54

305 Views

Sometimes we may be given a python list whose elements are tuples. Then we may have a data processing requirement which will need these tuples to be converted to lists for further processing. In this article, we will see how to convert a list of tuples to a list of lists.With list comprehensionIt is a straight forward approach where we create a for loop to loop through each element and apply the list function to create a list of lists.Example Live DemolistA = [('Mon', 3), ('Wed', 4), ('Fri', 7, 'pm')] # Given list print("Given list : ", listA) res = [list(ele) ... Read More

Convert list of tuples into list in Python

Pradeep Elance
Updated on 20-May-2020 11:04:17

675 Views

We may come across a lists whose elements are tuples. But for further data processing we may need to convert the tuples to the normal elements of a list. In this article we will see the approaches to achieve this.With list comprehensionIn this approach we design nested for loops to iterate through each tuple and produce the final list of elements.Example Live DemolistA = [('Mon', 3), ('Wed', 4), ('Fri', 7, 'pm')] # Given list print("Given list : ", listA) res = [item for t in listA for item in t] # Result print("Final list: ", res)OutputRunning the above code gives us ... Read More

Convert list of tuples into digits in Python

Pradeep Elance
Updated on 20-May-2020 11:02:52

108 Views

Python has a wide variety of data manipulation capabilities. We have a scenario in which we are given a list which has elements which are pair of numbers as tuples. In this article we will see how to extract the unique digits from the elements of a list which are tuples.With re and setWe can use regular expression module and its function called sub. It is used to replace a string that matches a regular expression instead of perfect match. So we design a regular expression to convert the tuples into normal strings and then apply the set function to ... Read More

Convert list of strings to list of tuples in Python

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

283 Views

While manipulating data with python, we may come across a list which has numbers as strings. Further we may want to convert the list of strings into tuples. Of course the given strings are in a particular format of numbers.With map and evalWe will use the map function to apply eval on every element of the list. Then store the final element as a list.Example Live DemolistA = ['21, 3', '13, 4', '15, 7'] # Given list print("Given list : ", listA) # Use eval res = list(map(eval, listA)) # Result print("List of tuples: ", res)OutputRunning the above code gives us ... Read More

Convert list of strings and characters to list of characters in Python

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

368 Views

While dalign with lists, we may come across a situation where we have to process a string and get its individual characters for further processing. In this article we will see various ways to do that.With list comprehensionWe design a for loop to go through each element of the list and another loop inside this to pick each character from the element which is a string.Example Live DemolistA = ['Mon', 'd', 'ay'] # Given lists print("Given list : ", listA) # Get characters res = [i for ele in listA for i in ele] # Result print("List of characters: ", res)OutputRunning ... Read More

Convert list of string into sorted list of integer in Python

Pradeep Elance
Updated on 20-May-2020 10:55:48

390 Views

Analysing data using python can bring us scenario when we have to deal with numbers represented as strings. In this article we will take a list which has numbers present as strings and we need to convert then to integers and then represent them in a sorted manner.With map and sortedIn this approach we apply the int function to every element of the list using map. Then we apply the sorted function to the list which sorts the numbers. It can handle negative numbers also.Example Live DemolistA = ['54', '21', '-10', '92', '5'] # Given lists print("Given list : ", listA) ... Read More

Convert list of numerical string to list of Integers in Python

Pradeep Elance
Updated on 20-May-2020 10:53:29

264 Views

For data manipulation using python, we may come across scenario where we have strings containing numbers in a list. To be able to make calculations, we will need to change the strings into numbers. In this article we will see the ways to change the strings into numbers inside a list.With intThe int function can be applied to the string elements of a list converting them to integers . We have to carefully design the for loops to go through each element and get the result even if there are multiple strings inside a single element.Example Live DemolistA = [['29', '12'], ... Read More

Advertisements