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.
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.
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))
.
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.
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"]].
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.
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}.
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.
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.
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.
Output:
['Hello, Ram how are you !', 'Hello, Abhishek how are you !',
'Hello, Rahul how are you !']
[480, 250, 120]
Top comments (0)