All possible LIST Idioms and Operations in Python
Tags: python
Published: 2022-03-14 18:49:06

I have added possible list operations/idioms available in Python.
Please note I have uploaded images of the code snippet as dev.to default code block offers lame black background with white text. You can find the following code into this github repo. 

## 1. String to List
Code snippet to accept a string paragraph and convert it into list. Might be the most easiest code in the python.


![String to list in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lp60ko3fqc0nrjr3499.jpg)

Output:
```
['Lorem', 'Ipsum', 'is', 'simply', 'dummy', 'text', 'of', 'the',
'printing', 'and', 'typesetting', 'industry.', 'Lorem', 'Ipsum', 
'has', 'been', 'the', "industry's", 'standard', 'dummy', 'text', 
'ever', 'since', 'the', '1500s,', 'when', 'an', 'unknown', 
'printer', 'took', 'a', 'galley', 'of', 'type', 'and', 
'scrambled', 'it', 'to', 'make', 'a', 'type', 'specimen', 'book.']
```

## 2. List to String
Not really a list operation but very helpful for begineers.


![List to string in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fixintvyw9wjnbbzudwh.jpg)

Output:
```
Lorem Ipsum is simply dummy text of the printing and typesetting 
industry. Lorem Ipsum has been the industry's standard dummy text 
ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book.
```

## 3. Integer to List
This code accept an integer and convert it into a list with all items being integer type. You can check the type of variable using `print(type(variable_name))`.


![Integer to list in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbro9mttzoxixw8wwn84.jpg)

Output:
```
[4, 5, 8, 9, 4, 1, 2, 5, 6]
```

## 4. List to Integer
You can do this too using the same strategy as in Step 2.



![List to integer in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuibf4vdux5nbpanq22f.jpg)

Output:
```
458941256
```

## 5. Dictionary to List
Dictionary is by far the most powerful python data-type for me. So, what I am gonna do is convert the dictionary items and values into a nested list for eg., [[1,"alpha"], [2,"beta"]].



![Dictionary to list in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4d1gygpm17eywos5xe7.jpg)

Output:
```
[[1, 'alpha'], [2, 'beta'], [3, 'gamma'], [4, 'sigma'], [5, 'omega']]
```

## 6. Tuple to List
This code will accept a nested tuple into nested list. 



![tuple to list in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytsztcay7tt2lfdy6rw3.jpg)

Output:
```
[[1, 'alpha'], [2, 'beta'], [3, 'gamma'], [4, 'sigma'], [5, 'omega']]
```

## 7. Find duplicates in the List
A code to find duplicate items in the list, find how many time it is repeated and put the detail into dictionary for eg., {'Lorem': 2, 'Ipsum': 2}.



![Find duplicates in the list in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3j4wp2bmtsvxmttlukfh.jpg)

Output:
```
{'Lorem': 2, 'Ipsum': 2, 'dummy': 2, 'text': 2, 'of': 2, 
'the': 3, 'and': 2, 'a': 2, 'type': 2}
```

## 8. Split String List into pairs
Split string into the pairs, what?
Say, "hello I am working" to ["hello I", "am working"].
Seems easy right? Let's do it.



![Split string list into pairs in python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrqa80pjs1ii61l2tmme.jpg)

Output:
```
['Lorem Ipsum', 'is simply', 'dummy text', 'of the', 'printing 
and', 'typesetting industry.', 'Lorem Ipsum', 'has been', "the 
industry's", 'standard dummy', 'text ever', 'since the', '1500s, 
when', 'an unknown', 'printer took', 'a galley', 'of type', 'and 
scrambled', 'it to', 'make a', 'type specimen', 'book.']
```

## 9. List can store Functions inside it
Yes you can store functions inside a list and access it the same way you access a list item, both using for-loop and through indices.



![python functions in a list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b77w6et25jha5nk1vsum.jpg)

Output:
```
Hello Rahul how are you !
I AM MAD!
```

## 10. Map function with list
In simple words, map accept two iterators and iterate one iterator item with other until no items left. So, I am going to be a little creative and using the greet function from last step for demonstration.



![python map function with list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bew03b9udolunrglrc04.jpg)

Output:
```
['Hello, Ram how are you !', 'Hello, Abhishek how are you !', 
'Hello, Rahul how are you !']
[480, 250, 120]
```