Python Articles

Page 767 of 852

Convert list of tuples into digits in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 238 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 and characters to list of characters in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 505 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 dictionary object into string in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 402 Views

For data manipulation in python we may come across situation to convert a dictionary object into a string object. This can be achieved in the following ways.with str()In this straight forward method we simple apply the str() by passing the dictionary object as a parameter. We can check the type of the objects using the type() before and after conversion.Example Live DemoDictA = {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"} print("Given dictionary : ", DictA) print("Type : ", type(DictA)) # using str res = str(DictA) # Print result print("Result as string:", res) print("Type of Result: ", type(res))OutputRunning the above ...

Read More

Convert case of elements in a list of strings in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 3K+ Views

As part of data manipulation, we will come across the need to have a single case for all the letters in a string. In this article we will see how to take a list which has string elements with mixed cases. We then apply some python functions to convert them all to a single case.With lower()The lower function is a string function that can convert the entire string to lower case. So we use lambda and map to apply the lower function to each of the elements in the list.Example Live DemolistA = ['MoN', 'TuE', 'FRI'] # Given list print("Given list ...

Read More

Convert byteString key:value pair of dictionary to String in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 1K+ Views

The byte string in python is a string presentd with letter b prefixed on it. In this article we will see how to convert a dictionary with the bytecode string into a normal dictionary which represents only strings.With decode and asciiPython string method decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding. We use it to convert the bytecode value into normal asci values by supplying ascii as the parameter to the decode function.Example Live Demobstring = {b'day': b'Tue', b'time': b'2 pm', b'subject': b'Graphs'} print(bstring) # Use decode stringA = {y.decode('ascii'): bstring.get(y).decode('ascii') for ...

Read More

Convert a string representation of list into list in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 14K+ Views

As python handles various data types, we will come across a situation where a list will appear in form of a string. In this article we will see how to convert a string into a list.With strip and splitWe first apply the strip method to remove the square brackets and then apply the split function. The split function with a comma as its parameter create the list from the string.Example Live DemostringA = "[Mon, 2, Tue, 5, ]" # Given string print("Given string", stringA) print(type(stringA)) # String to list res = stringA.strip('][').split(', ') # Result and its type print("final list", res) ...

Read More

Convert a list of multiple integers into a single integer in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 3K+ Views

Sometimes we may have a list whose elements are integers. There may be a need to combine all these elements and create a single integer out of it. In this article we will explore the ways to do that.With joinThe join method can Join all items in a tuple into a string. So we will use it to join each element of the list by iterating through them through a for loop.Example Live DemolistA = [22, 11, 34] # Given list print("Given list A: ", listA) # Use res = int("".join([str(i) for i in listA])) # Result print("The integer is : ...

Read More

Consecutive elements pairing in list in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 805 Views

During data analysis using python, we may come across a need to pair-up the consecutive elements of a list. In this article we will see the various ways to achieve this.With index and rangeWe will design an expression to put the consecutive indexes of the list elements together. And then apply the range function to determine the maximum number of times this pairing of consecutive elements will go on.Example Live DemolistA = [51, 23, 11, 45] # Given list print("Given list A: ", listA) # Use res = [[listA[i], listA[i + 1]]    for i in range(len(listA) - 1)] # Result ...

Read More

Consecutive element maximum product in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 380 Views

Python has great libraries to manipulate data. We may come across a need to find the maximum product of two consecutive numbers which are part of the big string. In this article we will see the ways to achieve that.With zip and maxWe convert the string into a list. Then create pairs from the consecutive elements with help of slicing. Applying * we multiply the pair and then take the max value from the result of the multiplication from each of the pairs.Example Live DemoAstring = '5238521' # Given string print("Given String : ", Astring) # Convert to list Astring = ...

Read More

Concatenate two lists element-wise in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 1K+ Views

Pyhton has great data manipulation features. In this article we will see how to combine the elements from two lists in the same order as they are present in the lists.With zipThe zip function can take in the two lists as parameters and concatenate them. We design a for loop to capture these combinations and put them into a new list.Example Live DemolistA = ["Outer-", "Frost-", "Sun-"] listB = ['Space', 'bite', 'rise'] # Given lists print("Given list A: ", listA) print("Given list B: ", listB) # Use zip res = [i + j for i, j in zip(listA, listB)] # Result ...

Read More
Showing 7661–7670 of 8,519 articles
« Prev 1 765 766 767 768 769 852 Next »
Advertisements