# Sort a List of Tuples Using the First Element Hey Pythonies! Today we shall study some advanced concepts about basic data structures lists and tuples. In this article, the main focus is on how we can sort a list of tuples using the first element. So let us get into it.

## What are Lists in Python?

Lists are one of the basic data structures that hold elements of different data types in a sequence. The sequence doesn’t need to follow an order. Lists in python skip the following points that can be problematic while programming.

1. Fixed size/non-dynamic.
2. Cannot hold objects/elements of dissimilar data types.
3. Needs a loop to display the entire array in runtime.
4. Longer and complex code operations.

Rather lists have the following characteristics:

1. Mutable – we can add, delete, edit, the elementsfrom anywhere in the list.
2. Sorting is easy – using the built in method sort() we can sort the elements in accending and descending. But, for sorting the elements should be of similar data types.
3. Dynamic – when we add a specific element in a list, Python reserves extra memory space for next element in each addition. This makes it dynamic.
4. Ease of access – by calling the list variable whole list gets displayed on the screen.

Code:

```list_1 = [34, 4, 5, -4, 5]  # list of integers
list_2 = ["one", "two", "three"]  # list of strings
list_3 = [45.7, 4.9, 0.0, -8]  # list of floating point numbers
list_4 = ['a', 'b', 'c', 'd'] # list of characters
list_5 = ["sam", 45, 90.0, 'a']    # a list of elements of different data types
```

When we print these elements in a row we get the following output

Output:

```>>> [34, 4, 5, -4, 5]
>>> ["one", "two", "three"]
>>> [45.7, 4.9, 0.0, -8]
>>> ['a', 'b', 'c', 'd']
>>> ["sam", 45, 90.0, 'a']
```

This is one of the most profitable ways to manipulate data. There are some built-in methods where certain operations like searching, sorting, etc are possible.

## What are Tuples in Python?

Tuples in Python is a list-like data structure. Just the differences are in syntax and some of the operations. A tuple is just helping us to enclose specific elements of dissimilar data types into a close non-editable container.

Following are the characteristics of a tuple:

1. Immutable: Once declared we cannot edit it. Neither we can add nor remove an item from it.
2. Fixed-size: We cannot make any size changes to it. This is due to the immutability property of tuples.
3. Can hold elements of dissimilar data types.

Syntax:

There are two ways we can declare a tuple:

```tup_1 = 1, 2, 3, 4
print(tup_1)
tup_2 = (1, 2, 3, 4)
print(tup_2)
```

Output:

```>>> (1, 2, 3, 4)
>>> (1, 2, 3, 4)
```

Adding parenthesis is one of the fundamental routines when we declare a tuple. But, if we just assign multiple values to a single variable, then by default it is considered as a tuple by the Python interpreter.

### Unpacking Tuples

When we unpack the tuples using iterables. There are two techniques.

• Using simple variables
```tup_1 = 1, 2, 3, 4
a, b, c, d = tup_1

print("Unpacking values from a tuple.....")
print("Value of a is: ", a)
print("Value of b is: ", b)
print("Value of c is: ", c)
print("Value of d is: ", d)
```

Output:

```>>> Unpacking values from a tuple.....
>>> Value of a is: 1
>>> Value of b is: 2
>>> Value of c is: 3
>>> Value of d is: 4
```
• Using wild cards
```a, b, *c, d = [11, 22, 33, 44, 55, 66, 77, 88, 99, 110]
print("Unpacking values from a tuple.....")
print("Value of a is: ", a)
print("Value of a is: ", b)
print("Value of a is: ", c)
print("Value of a is: ", d)
```

Output:

```>>> Unpacking values from a tuple.....
>>> Value of a is: 11
>>> Value of b is: 22
>>> Value of c is: (33, 44, 55,  66, 77, 88, 99)
>>> Value of d is: 110
```

Here the value of variable c is a tuple. This is because the wild card “*” helps us to assign a tuple of values to a variable.

## Easily Sort a List of Tuples in Python

As we all know that the tuple is an immutable type of data structure in Python. So, we need to do some explicit operations with it for the sorting purpose.

#### Make a list of tuples and then sort it.

Code:

```tup_1 = (11, -5, -56, 9, 4)
tup_2 = (3, 43, -1, 90.0)
tup_3 = (4.5, 3.0, 9.0, 23.0)

tupleOfTuples = (tup_1, tup_2, tup_3)
print("Printing a tuple of tuples...")
print(tupleOfTuples )
```

Output:

```>>> Printing a tuple of tuples...
>>> ((11, -5, -56, 9, 4), (3, 43, -1, 90.0), (4.5, 3.0, 9.0, 23.0))
```

Python provides the flexibility of creating nested tuples – or tuples of tuples. So, we create one namely – tupleOfTuples, and then print it on the screen. Then let us recast it into a list using a built-in function and then we shall achieve sorting.

```listOfTuples = list(tupleOfTuples)
print(listOfTuples)

listOfTuples.sort()
print("Sorted list of tuples: ", listOfTuples)

```

Output:

```>>> Raw list of tuples....
>>> [(11, -5, -56, 9, 4), (3, 43, -1, 90.0), (4.5, 3.0, 9.0, 23.0)]
>>> Sorted list of tuples...
>>> [(3, 43, -1, 90.0), (4.5, 3.0, 9.0, 23.0), (11, -5, -56, 9, 4)]
```

So here in this way, we create a list of tuples and sort their list using the first element of each tuple.

#### Convert To Dictionary And Perform Sort

A dictionary in Python is a collection of key-value pairs which is non-sequential. This makes it easy and accessible to use when we deal with complex data.

Code:

```nums = {4.0:5, 6.0:4, 90:3, 34:5}
a = nums.items()

for k,v in nums.items():
print(k, ":", v)

print(a)
```

Output:

```4.0 : 5
6.0 : 4
90 : 3
34 : 5

dict_items([(4.0, 5), (6.0, 4), (90, 3), (34, 5)])
```

Now that we have got our items list in the form of tuples let us make it an original list and then sort it.

```a = list(a)
print("Items list:", a)
print("Sorting the list of items...")
a.sort()
print("Returning a sorted list...")
print(a)
```

Output:

```Items list: [(4.0, 5), (6.0, 4), (90, 3), (34, 5)]
Sorting the list of items...
Returning a sorted lis...
[(4.0, 5), (6.0, 4), (34, 5), (90, 3)]
```

Let us cross-check whether we are right or wrong…

Code:

```for i in a:
print(i)
```

Here create a for loop and iterate through each element of list a. If the first element of each is greater than the previous first element of another one, then we can say that the tuple-list is sorted.

```(4.0, 5)
(6.0, 4)
(34, 5)
(90, 3)
```

### Complete Code To Sort A list of Tuples

Code1:

```tup_1 = (11, -5, -56, 9, 4)
tup_2 = (3, 43, -1, 90.0)
tup_3 = (4.5, 3.0, 9.0, 23.0)

tupleOfTuples = (tup_1, tup_2, tup_3)
print("Printing a tuple of tuples...")
print(tupleOfTuples )

listOfTuples = list(tupleOfTuples)
print(listOfTuples)

listOfTuples.sort()
print("Sorted list of tuples: ", listOfTuples)
```

Code 2:

```nums = {4.0:5, 6.0:4, 90:3, 34:5}
a = nums.items()

for k,v in nums.items():
print(k, ":", v)

print(a)

a = list(a)
print("Items list:", a)
print("Sorting the list of items...")
a.sort()
print("Returning a sorted list...")
print(a)

for i in a:
print(i)
```

## Conclusion

In this way, we saw how we can sort the list of tuples using the first element using two ways. The basics of how a list and a tuple are declared are also covered in this article. All this adds a basic understanding of those data structures in python. All the best and happy coding.