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

КОМЕНТАРІ • 18

  • @NKARANAMLAKSHMI
    @NKARANAMLAKSHMI Рік тому +1

    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.

  • @visheshsingh601
    @visheshsingh601 Рік тому +1

    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)

  • @LearningBigdata
    @LearningBigdata Рік тому +1

    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]

  • @srikanthswarnaganti-xi7qf
    @srikanthswarnaganti-xi7qf Рік тому +3

    It worked excellent broh it cleared every testcase of my code

  • @bojanexpro2224
    @bojanexpro2224 Рік тому +3

    You solved my life problems thxsm🔥

  • @nileshyadav7935
    @nileshyadav7935 Рік тому +2

    Very very thankful to you sir.....🙌🙌

  • @mohitsgit2006
    @mohitsgit2006 2 роки тому +1

    In the first approach, we are getting the same results if we pass the index numbers directly instead of first assigning them to variables

  • @kshitijfulzele6703
    @kshitijfulzele6703 4 роки тому +1

    can we do approach 1 and 2 to alphabetical characters or is it just limited to integers

  • @user-ej9zw4zv8g
    @user-ej9zw4zv8g 3 роки тому +2

    thank you so much

  • @Cheers14a
    @Cheers14a 2 роки тому

    with third temporary variable also we can do swapping

  • @kyleward7417
    @kyleward7417 Рік тому

    Pos1 and Pos2 are user defined not build in so how does python figure it's the index of the list?

  • @kvelez
    @kvelez 11 місяців тому

    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)

  • @patricktang3377
    @patricktang3377 Рік тому

    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)

    • @MeVsThem_Timelapse
      @MeVsThem_Timelapse Рік тому

      it might be easier logically to define a third variable, but time complexity is higher, and python has inbuilt libraries so no need tbh