
daily coding
@dailypython101
I'm learning coding with Python. If you follow me you will receive a Python code snippet every once in a while.
We can iterate over three lists simultaneously and use zip to combine them. integers = [1, 2, 3] letters = ["a", "b", "c"] floats = [4.0, 5.0, 6.0] for I, l, f in zip(integers,letters,floats): print(I, l, f) # Output: 1 a 4.0 2 b 5.0 3 c 6.0
Use reversed () to return the reversed object that can be turned into list using list(): y = [7, 3, 2,] z = list(reversed(y)) print(z) # Output: [2, 3, 7] 3. Iterate through the sorted list and print them out in a reversed way: for i in reversed(y): print(i) # Output: 2 3 7
1. use sorted() to return the sorted list: y = [7, 3, 2, 4, 6, 5, 1] print(sorted(y)) # Output: [1, 2, 3, 4, 5, 6, 7] 2. We can iterate through the sorted list and print them out in a sorted way x = [1, 2, 3, 4, 5, 6] for i in sorted(x): print(I) # Output: 1 2 3 4 5 6
How to find the index of specific elements in the list x = [1, 2, 3, 4, 5, 6] q = x.index(3) # The index of 3(elements' name) is saved in q print(q) # Output: 2
How to delete data from a list using .remove() x = [1, (22, 33), 66, 2, 3, 4, 5, 6, (22, 33), 22, 33] x.remove((22,33)) # It removes the chosen element in the list if we pass the element as an argument print(x) # Output: [1, 66, 2, 3, 4, 5, 6, (22, 33), 22, 33]
Delete data from a list using .pop() X = [1, 3, 66] X.pop() # removes element from the end print(X) Output:[1,3] X.pop(0) # It removes the chosen element print(X) Output:[3] q = x.pop(0) # If you assign the x.pop() to a variable, you can save it. print(q) Output:3
Hello again 1. How many specific element is inside a list x = [1, 1, 66] print(x.count(1)) Output: 2 --> two 1 inside the x list 2. Access the position of an element in a list using index(): x = [1, 3, 66] q = x.index(3) # 3 is in index 1 print(q) Output: 1 #tutorial
How to add new data to a list: x = [1, 2, 3, 4, 5, 6] x.append(33) print(copy) x = [1, 2, 3, 4, 5, 6] x.append((22,33)) print(copy) x = [1, 2, 3, 4, 5, 6] x.extend((22,33)) print(x) x = [1, 2, 3, 4, 5, 6] x.insert(1, 66) print(x)
How to access one or more items in an existing list: # Access an element in a list print(x[4]) # Access range of elements print(x[0:5:2]) # Access specific elements in a list w = list() w.extend([x[0], x[3]]) print(w) OR w = list() for i in [x[0], x[3]]: w.append(i) print(w)
how to create a list: x = [1, 2, 3, 4, 5, 6] y = list((1,2,3)) z = [2 * number for number in x] w = list() w.append(5) w.append(6) w.append("Python") w.extend((3.5,"book")) print(w)
United States Trends
- 1. Ohtani 196K posts
- 2. Dodgers 241K posts
- 3. Dodgers 241K posts
- 4. Carson Beck 16K posts
- 5. $SAWA 1,655 posts
- 6. Miami 99.1K posts
- 7. Louisville 27.4K posts
- 8. Nebraska 17.6K posts
- 9. Brewers 54.3K posts
- 10. Babe Ruth 3,049 posts
- 11. NLCS 55.9K posts
- 12. #SmackDown 55.6K posts
- 13. #BostonBlue 7,699 posts
- 14. Rhule 4,475 posts
- 15. 3 HRs 9,941 posts
- 16. Minnesota 47.7K posts
- 17. George Santos 78.6K posts
- 18. 10 Ks 4,228 posts
- 19. Emiru 4,449 posts
- 20. Jeff Brohm 3,013 posts
Something went wrong.
Something went wrong.