DEV Community

Rahul Kumar
Rahul Kumar

Posted on • Edited on

All possible LIST Idioms and Operations in Python

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

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.']
Enter fullscreen mode Exit fullscreen mode

2. List to String

Not really a list operation but very helpful for begineers.

List to string in python

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.
Enter fullscreen mode Exit fullscreen mode

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

Output:

[4, 5, 8, 9, 4, 1, 2, 5, 6]
Enter fullscreen mode Exit fullscreen mode

4. List to Integer

You can do this too using the same strategy as in Step 2.

List to integer in python

Output:

458941256
Enter fullscreen mode Exit fullscreen mode

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

Output:

[[1, 'alpha'], [2, 'beta'], [3, 'gamma'], [4, 'sigma'], [5, 'omega']]
Enter fullscreen mode Exit fullscreen mode

6. Tuple to List

This code will accept a nested tuple into nested list.

tuple to list in python

Output:

[[1, 'alpha'], [2, 'beta'], [3, 'gamma'], [4, 'sigma'], [5, 'omega']]
Enter fullscreen mode Exit fullscreen mode

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

Output:

{'Lorem': 2, 'Ipsum': 2, 'dummy': 2, 'text': 2, 'of': 2, 
'the': 3, 'and': 2, 'a': 2, 'type': 2}
Enter fullscreen mode Exit fullscreen mode

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

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.']
Enter fullscreen mode Exit fullscreen mode

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

Output:

Hello Rahul how are you !
I AM MAD!
Enter fullscreen mode Exit fullscreen mode

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

Output:

['Hello, Ram how are you !', 'Hello, Abhishek how are you !', 
'Hello, Rahul how are you !']
[480, 250, 120]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)