Frequently Asked Python Program 9: How To Swap Any 2 Elements of a List
Вставка
- Опубліковано 12 вер 2024
- Topic : How To Swap Any 2 Elements of a List
#########################
Udemy Courses:
#########################
Manual Testing+Agile with Jira Tool
************************************
► shorturl.at/xNXZ4
Selenium with Java+Cucumber
********************************
► shorturl.at/aIKMW
Selenium with Python & PyTest
********************************
► shorturl.at/ELW09
Selenium with python using Robot framework
****************************************
► shorturl.at/bcDPZ
API Testing(Postman, RestAssured & SoapUI)
*****************************************
► shorturl.at/uAWY7
Web & API Automation using Cypress with Javascript
********************************************
► rb.gy/4uum2n
Playwright with Javascript
**************************
► shorturl.at/gALZ1
Jmeter-Performance Testing
************************
► rb.gy/ocj7r2
SDET Essencials(Full Stack QA)
*************************
► rb.gy/hlnf2v
Appium-Mobile Automation Testing
************************************
► rb.gy/wmzv64
Java Collections
*****************
► bit.ly/3S66ezE
Python Programming
*********************
► urlis.net/gdsf...
Cucumber BDD Framework
***************************
► bit.ly/3F7d69r
Protractor with Javascript
***************************
► urlis.net/4udd...
####################################
UA-cam Playlists:
####################################
Manual Testing & Agile
***********************
► bit.ly/3ujvaYa
► bit.ly/3OYzxQj
SQL
*************************
► bit.ly/3R6XjeG
► bit.ly/3nE0a1s
► bit.ly/3IeKuuJ
linux & Shell Scripting
**********************
► bit.ly/3up1Z6a
► bit.ly/3nzvGxx
Java
**********************
► bit.ly/3R9QfOs
► bit.ly/3AoRMKM
► bit.ly/3IbTTDn
► bit.ly/3ybXInv
► bit.ly/3yCoHdw
Selenium With Java+Cucumber
********************************
► bit.ly/3P2FMm4
► bit.ly/3yhiIt4
► bit.ly/3uokzet
► bit.ly/3a7XIND
► bit.ly/3umN2S9
► bit.ly/3aar7GW
► bit.ly/3y9kD2S
► bit.ly/3uhLskn
Python
********************************
► bit.ly/3agNgU0
► bit.ly/3NJNnoy
► bit.ly/2CTQ4mR
► bit.ly/3OIYWh1
Selenium With Python,Pytest&Behave
***************************************
► bit.ly/3OHHoC9
► bit.ly/3IeNLdv
► bit.ly/2J4tPeT
► bit.ly/3ydSkAq
Selenium With Python Using Robert Framework
(Web&API Testing)
*************************************************
► bit.ly/3nUvlpr
► bit.ly/3nUvE3z
API Testing (Postman,SoapUi,&Rest Assured)
**********************************************
► bit.ly/3OXacWY
► bit.ly/3yh0UhE
► bit.ly/3nC9DWQ
► bit.ly/3yGfhNS
► bit.ly/3OJa11H
► bit.ly/3P2slCC
Mobile App Testing Appium
****************************
► bit.ly/3Al49HG
Performance Testing Jmeter
*******************************
► bit.ly/3nz4fE7
Maven,Jenkins,Git,Github,CI/CD
*******************************
► bit.ly/3P16HPj
► bit.ly/3yhlUVA
► bit.ly/3afYfwT
► bit.ly/3yGzweG
► bit.ly/3yF73Ws
► bit.ly/3NCOmH7
SQL,DB Testing&ETL,Bigdata
*******************************
► bit.ly/3NKOcNY
► bit.ly/3OVpDyI
► bit.ly/3NEvUy0
► bit.ly/3IryxlT
► bit.ly/3RkWBLh
JavaScript Based Automation Tools
********************************
► bit.ly/3nUx51X
► bit.ly/3AqI6Q0
► bit.ly/3yeurbT
► bit.ly/3ujzpTK
Selector Hub Tools
********************
►rb.gy/h3h424
GraphQL
******************
► rb.gy/sj3gys
Cypress API Testing
********************
► shorturl.at/fjKPX
Cypress Web Testing
**********************
► shorturl.at/agjlK
Playwright with Javascipt
**************************
► shorturl.at/adSTY
#PythonSwapElements
#ListElementSwap
#PythonListManipulation
#SwapInPython
#ListSwapping
#PythonProgramming
#PythonCoding
#ListManipulation
#PythonTips
#CodingSolutions
#ProgrammingHelp
#PythonTricks
#ListOperations
#PythonBeginners
#CodingCommunity
#ListAlgorithms
#PythonLists
#AlgorithmPractice
#ProgrammingLogic
#CodeExamples
#PythonSnippets
#ListHandling
#SwapAlgorithm
#LearnPython
#CodingInPython
#ProgrammingProblems
#TechInterview
#PythonProblems
#CodingChallenges
#ListSwappingTechniques
#PythonHelp
#AlgorithmExplained
#CodingExploration
#PythonTricksAndTips
#ListModification
#SwapFunction
#PythonLogic
#Programming101
#ListManipulationTricks
#PythonDevelopment
#CodeLearning
#AlgorithmicThinking
#PythonListTricks
#ProblemSolving
#CodeOptimization
#ListSwappingMethods
#PythonCommunity
#CodingGuidance
#ListCoding
#PythonProgrammingTips
In the second approach, it is working for the index values only if we enter pos1 < pos2, if pos1>pos2 it shifts the second position to the pos2-1............Ex: pos1=5 and pos2=3......... 5th position element popped and then 3 rd position shifting to 2 nd position and then it is swapping.
arr=[]
a=int(input("Enter how many elements you want in array"))
for i in range(1,a+1):
b=int(input())
arr.append(b)
pos1=int(input("enter first position"))
pos2=int(input("enter second position"))
arr[pos1-1],arr[pos2-1]=arr[pos2-1],arr[pos1-1]
print(arr)
Thank you for the wonderful video series. May I ask whether the below approach in acceptable since in the starting of the video the swap was shown based on the actual position instead of index value? Kindly suggest.
def posSwap(list_values:list, pos1:int, pos2:int):
pos_index1, pos_index2 = pos1 -1, pos2 - 1
list_values[pos_index1], list_values[pos_index2] = list_values[pos_index2], list_values[pos_index1]
return list_values
print(posSwap([1,2,3,4,5],1,4))
# Result: [4, 2, 3, 1, 5]
It worked excellent broh it cleared every testcase of my code
You solved my life problems thxsm🔥
Welcome
Very very thankful to you sir.....🙌🙌
Welcome
In the first approach, we are getting the same results if we pass the index numbers directly instead of first assigning them to variables
can we do approach 1 and 2 to alphabetical characters or is it just limited to integers
The first approach should work with strings.
thank you so much
Welcome
with third temporary variable also we can do swapping
Pos1 and Pos2 are user defined not build in so how does python figure it's the index of the list?
Methods 1:
arr = [1,2,3,4,5]
first = arr[0]
last = arr[-1]
arr.insert(0, last)
arr.pop()
arr.append(first)
arr.pop(first)
print(arr)
=====================================
Method 2:
arr = [1,2,3,4,5]
first = arr[0]
last = arr[-1]
arr.insert(0, last)
arr.pop()
arr.append(first)
arr.remove(first)
print(arr)
There is an easier way to do this. Try this:
my_list = [23,65,19,90]
temp = my_list[1] # temp = 65
my_list[1] = my_list[3] # my_list[1] = 90
my_list[3] = temp # my_list[3] = 65
print(my_list)
it might be easier logically to define a third variable, but time complexity is higher, and python has inbuilt libraries so no need tbh