22 Ways To Use Lists In Python (3 Min) | Python List Tutorial

Опубликовано: 20 Февраль 2022
на канале: Gokce DB
78
5

In this tutorial, you'll learn 22 ways to use lists in Python:

Facebook:   / gokcedbsql  

Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn 22 things you can do with the list in Python. Let's start by declaring a list with some initial values. Here I'm using integers and some strings.

Number one, use the print function to print a list. Here I'm also using an STR function to convert the list into a string so I can concatenate it with another string. Number two, to print the third item in the list use the index value of two square brackets that's because the index starts from a zero.

Number three, to print the last item of the list use the index value of minus one. Number four, say you wanted to print the items at index one, two, three, and four. To do that, use the slice notation.

The slice notation syntax of the slice notation is to start the index colon and index. The start index is inclusive and the end index is exclusive that's the reason why the item at index five was not included in the output. Number five if you don't include the start index in the slice notation it will default to zero.

Number six, if we don't include the end index in the slice notation it will default to the last item of the list. Number seven is to print the complete list using the colon in the square brackets. Number eight, to print every second item starting from index 0 use the slice notation of colon colon 2 in square brackets.

Number nine is to update the item at index 3 in the list. Use the equal to operator. Number 10, to append a new at the end of the list use the append method. Say you wanted to delete the item at index 3 and also get rid of the last item in the list.

To do that use the DEL keyword. Number 2 uses the extend method to combine two lists. Number 13 the end keyword is used to check whether an item exists in a list. Number 14 says you wanted to insert an item at index 3.

To do that make use of the insert method. Number 15, uses the len keyword to get the number of items in a list. Number 16 suppose you want to get the value of an item before removing it from the list.

To do that use the POP method. Number 17 makes use of the remove method, to remove an item from a list. Number 18 to reverse a list leverage the reverse method.

Number 19 to sort a list with all numeric values, use the sort method. Number 20 to return a new sorted list without sorting the original. Make use of the sorted function.

Number 21, to concatenate a list without modifying it use plus operator. Last but not the least number 22, to duplicate a list and concatenate it without modifying it use the star operator. There you have it.

Make sure you like, subscribe, and turn on the notification bell. Until next time.

1. print entire list
print("1: " + str(mySQLList))
2. print the third item
print("2: " + str(mySQLList[2]))
3. print the last item
print("3: " + str(mySQLList[-1]))
4. print index 1 to 4 using the slice notation
print("4: " + str(mySQLList[1:5]))
5. print index 0 to 4 using the slice notation
print("5: " + str(mySQLList[:5]))
6. print index 2 to the last item using the slice notation
print("6: " + str(mySQLList[2:]))
7. print the complete list using the slice notation
print("7: " + str(mySQLList[:]))
8. print every second item from index 0 using the slice notation
print("8: " + str(mySQLList[::2]))
9. update index 3, set to 4, then print updated list
mySQLList[3] = 4
print("9: " + str(mySQLList))
10. append a new item at the end of the list, then print updated list
mySQLList.append('cursors')
print("10: " + str(mySQLList))
11. remove index 3, remove last item using the del keyword, then print updated list
del mySQLList[3]
del mySQLList[-1]
print("11: " + str(mySQLList))
12. combine two lists using extend
mySQLListNew = ['triggers']
mySQLList.extend(mySQLListNew)
print("12: " + str(mySQLList))
13. check if an item is in a list
print("13: " + str('views' in mySQLList))
14. insert an item at a particular position
mySQLList.insert(3, 4)
print("14: " + str(mySQLList))
15. print the number of items in a list
print("15: " + str(len(mySQLList)))
16. get the value of an item and remove it from the list using the pop method
item = mySQLList.pop(3)
print("16: removed: " + str(item) + " updated list: " + str(mySQLList))
17. remove an item from a list using the remove method
mySQLList.remove('triggers')
print("17: " + str(mySQLList))
18. reverse a list using the reverse method
mySQLList.reverse()
print("18: " + str(mySQLList))
19. sort a list with all numeric items using the sort method
mySQLList1 = [-1, -2, 1, 2, 0]
mySQLList1.sort()
print("19: " + str(mySQLList1))
20. return a new sorted list without sorting the original
mySQLList1 = [-1, -2, 1, 2, 0]
mySQLList2 = sorted(mySQLList1)
print("20: mySQLList1: " + str(mySQLList1) + " mySQLList2: " + str(mySQLList2))


Смотрите видео 22 Ways To Use Lists In Python (3 Min) | Python List Tutorial онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Gokce DB 20 Февраль 2022. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 78 раз и оно понравилось 5 посетителям.