Tag: Tuples
Python: How to sort a list of tuples by 2nd Item using Lambda Function or Comparator
In this article we will discuss how to sort a list of tuple using 2nd or i th item of tuple. Suppose we have a list of tuple i.e. words and thier frequency count, Advertisements # List of tuples i.e. word and its frequency count wordFreq = [ (‘the’ , 34) , (‘at’ , 23), …
Python: How to sort a list of tuples by 2nd Item using Lambda Function or ComparatorRead More
Python: Read CSV into a list of lists or tuples or dictionaries | Import csv to list
In this article we will discuss how to import a CSV into list. This list can be a list of lists, list of tuples or list of dictionaries. We will also use pandas module and cover scenarios for importing CSV contents to list with or without headers. Suppose we have a CSV file ‘students.csv’ and …
Python: Read CSV into a list of lists or tuples or dictionaries | Import csv to listRead More
Python: Convert dictionary to list of tuples/ pairs
In this article, we will discuss different ways to convert all key-value pairs of a dictionary to a list of tuples. Table of Contents Convert all pairs of dictionary to list of tuples using items() Use zip() to convert all pairs of dictionary to list of tuples Use List comprehension to convert all pairs of …
Python: Convert dictionary to list of tuples/ pairsRead More
Convert a list of tuples to list of lists in Python
In this article, we will discuss different ways to create a list of lists from a list of tuples in python. Table Of Contents Method 1: Using List Comprehension Method 2: Using for-loop Method 3: Using map() function Method 4: Using enumerate() function Summary Method 1: Using List Comprehension Inside a List Comprehension, iterate over …
Convert a list of tuples to list of lists in PythonRead More
Print a list of tuples in Python
In this article, we will discuss how to print a list of tuples in Python. Table Of Contents Method 1: Print list of tuples using for-loop Example 1 Example 2 Method 2: Print list of tuples with header row Method 3: Print list of tuples using join() function Summary Method 1: Print list of tuples …
Convert a List of Tuples to a List in Python
This tutorial will discuss about unique ways to convert a list of tuples to a list in Python. Table Of Contents Method 1: Using List Comprehension Method 2: Using itertools chain() Method 3: Using reduce() function Summary Method 1: Using List Comprehension We are going to use two nested for loops inside List Comprehension. Advertisements …
Convert a List of Tuples to two Lists in Python
This tutorial will discuss about unique ways to convert a list of tuples to two lists in Python. Table Of Contents Introduction Method 1: Using zip() and map() functions Method 2: Using List Comprehension Method 3: Using List Comprehension Summary Introduction Suppose we have a list of tuples, and each tuple has two elements i.e. …
Convert a list of tuples to a list of lists in Python
This tutorial will discuss about unique ways to convert a list of tuples to a list of lists in Python. Table Of Contents Method 1: Using List Comprehension Method 2: Using map() function Method 3: Using enumerate() function Summary Method 1: Using List Comprehension Iterate over all the tuples in list using List Comprehension. During …
Convert a list of tuples to a list of lists in PythonRead More
Convert a list of tuples to a dictionary in Python
This tutorial will discuss about unique ways to convert a list of tuples to a dictionary in Python. Table Of Contents Method 1: Using for loop Method 2: Using setdefault() Summary Suppose we have a list of tuples, in which each tuple has a Name and a number i.e. Advertisements listOfTuples = [(‘Mark’, 34), (‘John’, …
Convert a List of Lists to a List of Tuples
This tutorial will discuss about unique ways to convert a list of lists to a list of tuples. Suppose we have a list of lists. Like this, listOfLists = [[‘John’, 34], [‘Mark’, 23], [‘Suse’, 37], [‘Avni’, 29], [‘John’, 100], [‘John’, 102]] We want to convert this list of lists into a list of tuples, like …